Deserializing JSON in Java using Jackson - java

I have the following sample fragment of JSON I am trying to deserialize.
{
"total": 2236,
"issues": [{
"id": "10142",
"key": "ID-2",
"fields": {
"attachment": [{
"id": "11132"
}]
}
}]
}
I can deserialize the data up to id and key, but cannot deserialize attachments that is in fields. My attachment class is always null
Here's my code.
Response.java
#Data
#JsonIgnoreProperties(ignoreUnknown = true)
public class Response {
#JsonProperty
private int total;
#JsonProperty
private List<Issue> issues;
}
Issue.java
#Data
#JsonIgnoreProperties(ignoreUnknown = true)
public class Issue {
#JsonProperty
private int id;
#JsonProperty
private String key;
#JsonProperty
private Fields fields;
}
Fields.java
#Data
#JsonIgnoreProperties(ignoreUnknown = true)
public class Fields {
#JsonProperty
private Attachments attachment;
}
Attachments.java
#Data
#JsonIgnoreProperties(ignoreUnknown = true)
public class Attachments {
#JsonProperty
private List<Attachment> attachment;
}
Attachments.java
#Data
#JsonIgnoreProperties(ignoreUnknown = true)
public class Attachment {
#JsonProperty
private String id;
}

In your JSON, attachment is an array, not an object.
You don't need an Attachments class, just modify Fields this way:
#Data
#JsonIgnoreProperties(ignoreUnknown = true)
public class Fields {
#JsonProperty
private List<Attachment> attachment;
}

Think of each variable in your Java classes as corresponding to an attribute in the JSON. "Attachments" is not in the JSON file. You should be able to remove it and change the variable definition in the Fields class.
#Data
#JsonIgnoreProperties(ignoreUnknown = true)
public class Fields {
#JsonProperty
private List<Attachment> attachment;
}

If you don't want to change class structure then, you can modify JSON.
In attachment, you need to add again an attachment.
JSON would be like below.
{
"total":2233,
"issues":[
{
"id":"13598",
"key":"ID-2368",
"fields":{
"attachment":{
"**attachment**":[
{
"id":"11122"
}
]
}
}
}
]
}

Related

Inner object deserialization with Jackson

I have a json
{
"params": [
{
"key": "path",
"options": {
"string": {
"prefix": "test_pref"
}
},
"default": {
"url": ""
}
}
]}
I have the following POJO class, where i want to map inner objects like option.string.prefix in json to prefix in Params POJO class.
#Data
#Accessors(chain = true)
public class Data {
private List<Params> params;
}
#Data
#Accessors(chain = true)
public class Params {
private String key;
#JsonProperty("options.string.prefix")
private String prefix;
#JsonProperty("default.url")
private String url;
}
Is there any Jackson annotation that helps me do this without #JsonProperty?
The is #JsonGetter which is an alternative to #JsonProperty You can read a very nice article on the topic here: Jackson Annotation Examples

Ignore enclosing braces with JSON parser while serializing object in Java

I have the following classes:
#JsonIgnoreProperties(ignoreUnknown = true)
#JsonInclude(JsonInclude.Include.NON_NULL)
public class User {
private String id;
private List<Reference> references;
.....
}
#JsonIgnoreProperties(ignoreUnknown = true)
#JsonInclude(JsonInclude.Include.NON_NULL)
public class Reference {
#JacksonXmlProperty(isAttribute = true)
private String ref;
public Reference(final String ref) {
this.ref = ref;
}
public Reference() { }
public String getRef() {
return ref;
}
}
When serializing to XML the format is as expected, but when I try to serialize to JSON I get the following
"users" : [
{
"references" : [
{
"ref": "referenceID"
}
]
}
]
And I need it to be:
"users" : [
{
"references" : [
"referenceID"
]
}
]
the braces enclosing the reference list I need it to be ignored without the attribute name
You can annotate the ref field in your Reference class with the JsonValue annotation that indicates that the value of annotated accessor is to be used as the single value to serialize for the instance:
#Data
#JsonIgnoreProperties(ignoreUnknown = true)
#JsonInclude(JsonInclude.Include.NON_NULL)
public class Reference {
#JacksonXmlProperty(isAttribute = true)
#JsonValue //<-- the new annotation
private String ref;
public Reference(final String ref) {
this.ref = ref;
}
public Reference() { }
public String getRef() {
return ref;
}
}
User user = new User();
user.setReferences(List.of(new Reference("referenceID")));
//it prints {"references":["referenceID"]}
System.out.println(jsonMapper.writeValueAsString(user));
Edit: it seems that the JsonValue annotation invalidates the serialization of the class as expected by the OP; to solve this problem one way is the use of a mixin class for the Reference class and putting inside the JsonValue annotation, the original Reference class will be untouched:
#Data
public class MixInReference {
#JsonValue
private String ref;
}
ObjectMapper jsonMapper = new ObjectMapper();
//Reference class is still the original class
jsonMapper.addMixIn(Reference.class, MixInReference.class);
////it prints {"references":["referenceID"]}
System.out.println(jsonMapper.writeValueAsString(user));

How do i ignore a specific field from an list in JSON response

I am trying to ignore a specified field from a list during deserialization. I am not sure how do i do that for a field that sits inside a list. Below is my json and response class
Sample json
{
"key": {
"rowKey": "123"
},
"names": [
{
"firstName": "JON ",
"firstNameFormatted": "JON"
}
]
}
Response class
#AllArgsConstructor
#NoArgsConstructor
#Getter
#Setter
#ToString
public class Data {
private Map<String,Object> key;
private List<Map<String,Object>> names;
}
Here i would like to ignore
firstNameFormatted
from my json response but i am not sure how to do that using jackson for a field that is inside a list ?
Jackson has a solution for that. Simply use #JsonIgnore
You can see it in the example below
#JsonIgnore
public String getPassword() {
return password;
}
Now the password information won’t be serialized to JSON.
Also you can try with #JsonIgnoreProperties

Convert multiple json object into array of object in Java

My data is currently stored in this format:
{
"label":["X","Y"],
"data":{
"site1":{
"week":[
{
"idWeek":"9",
"max":2,
"min":0
}
]
},
"site2":{
"week":[
{
"idWeek":"9",
"max":2,
"min":0
}
]
}
}
}
And I need to convert it into this format:
{
"label":["X","Y"],
"myClient":{
"week":[
{
"id":"9",
"access":2,
"lost":0
}
]
}
}
As you can see, I also need to take the keys (site name) and I need remove "data" and change name of property.
Any ideas on how I can do this in Java (I'm using Java 8 with Spring Boot? I'm not very good at restructuring that type of data.
UPDATE
My solutions: I did different interfaces and I used RestTemplate! I don't know it's the best solution however it worked.
public Optional<Consolidate> getCasosPorEstado() {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Consolidate> res = restTemplate.exchange(PATH_CONSOLIDATE, HttpMethod.GET, null,
new ParameterizedTypeReference<Consolidate>() {
});
return Optional.of(res.getBody());
}
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonPropertyOrder({ "label", "data" })
#Data
public class Consolidate {
#JsonProperty("label")
private List<String> labels = null;
#JsonProperty("data")
private MyClient client;
}
#Data
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonPropertyOrder({ "site1" })
class MyClient {
#JsonProperty("Site")
Site SiteObject;
}
#Data
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonPropertyOrder({ "week" })
class MS {
#JsonProperty("week")
private List<Week> week = null;
}
#Data
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonIgnoreProperties(ignoreUnknown = true)
public class Semana {
#JsonAlias("idWeek")
private String id;
#JsonAlias("max")
private Double access;
#JsonAlias("min")
private Double lost;
}

Why are some of the variables in POJO equal to null after converting JSON RESTful Webservice?

I am consuming a RESTful webservice that returns a JSON payload. I can successfully consume the RESTful webservice and manage to populate some of the POJO attributes with JSON data. However, some other attributes are null when they are supposed to contain a value. How can I ensure that there are no more nulls?
I have defined 4 POJO classes. I have so far debugged by systematically by testing the variables for each class. This is using Springboot 2.2.0 and Jackson-databind.
The JSON schema I am trying to consume:
{
"items":[
{
"timestamp":"2019-09-18T16:42:54.203Z",
"carpark_data":[
{
"total_lots":"string",
"lot_type":"string",
"lots_available":"string"
}
]
}
]
}
For the above, I defined 4 classes:
public class Response {
#JsonProperty
private List<items> i;
#JsonIgnoreProperties(ignoreUnknown = true)
public class items {
private String timestamp;
private List<carpark_data> cpd;
#JsonIgnoreProperties(ignoreUnknown = true)
public class carpark_data {
private List<carpark_info> cpi;
private String carpark_number;
private String update_datetime;
#JsonIgnoreProperties(ignoreUnknown = true)
public class carpark_info {
private int total_lots;
private String lot_type;
private int lots_available;
When I run the below in Spring boot Main: I get null. Is my POJO modeling OK?
Response resp = restTemplate.getForObject("")
c = resp.getItems().get(0).getCarpark_data().get(0);
log.info("The last update time for the car park data = " +
c.getUpdateDatetime());
Your model does not fit to JSON payload. If we assume that JSON payload has a structure like below:
{
"items": [
{
"timestamp": "2019-09-18T16:42:54.203Z",
"carpark_data": [
{
"total_lots": "1000",
"lot_type": "string",
"lots_available": "800"
}
]
}
]
}
We can deserialise it as below:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.util.List;
public class JsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
ObjectMapper mapper = new ObjectMapper();
Response response = mapper.readValue(jsonFile, Response.class);
System.out.println(response.getItems().get(0).getData().get(0));
}
}
class Response {
private List<Item> items;
//getters, setters, toString
}
class Item {
private String timestamp;
#JsonProperty("carpark_data")
private List<CarParkInfo> data;
//getters, setters, toString
}
class CarParkInfo {
#JsonProperty("total_lots")
private int totalLots;
#JsonProperty("lot_type")
private String lotType;
#JsonProperty("lots_available")
private int lotsAvailable;
//getters, setters, toString
}
Above code prints:
CarParkInfo{totalLots=1000, lotType='string', lotsAvailable=800}
Hope you find the solution.
It is in POJO, you need to check the fieldName and object structure.
Seeing the Json above, your response model returns list of items and in each item you have list of carpark_data. So, basic modelling should be like this. And you can include respective setter and getter.
public class Response {
#JsonProperty
private List<items> items;
#JsonIgnoreProperties(ignoreUnknown = true)
public class items {
private String timestamp;
private List<carpark_data> carpark_data;
#JsonIgnoreProperties(ignoreUnknown = true)
public class carpark_data {
private int total_lots;
private String lot_type;
private int lots_available;
}
You need to have fields name in POJO class same in the Json response or you can set JsonProperty for that field. Like this
#JsonProperty("items")
private List<items> i;
#JsonProperty("carpark_data")
private List<carpark_data> cpd;

Categories