How to deserialize nested JSON using GSON - java

JSON
"entities": {
"nca_location": [
{
"confidence": 0.85126983589896,
"value": "struga",
"type": "value"
},
{
"confidence": 0.9188255680843,
"value": "Skopje",
"type": "value"
}
],
"intent": [
{
"confidence": 0.99990092463824,
"value": "nca_get_position_availability"
}
]
}
EntityDto
public class EntityDto {
private String name;
private List<EntityValueDto> values;
public EntityDto(String name, List<EntityValueDto> values) {
this.name = name;
this.values = values;
}
public List<EntityValueDto> getValues() {
return values;
}
public void setValues(List<EntityValueDto> values) {
this.values = values;
}
}
EntityValueDto
public class EntityValueDto {
private Float confidence;
private String value;
public EntityValueDto(Float confidence, String value) {
this.confidence = confidence;
this.value = value;
}
public Float getConfidence() {
return confidence;
}
public void setConfidence(Float confidence) {
this.confidence = confidence;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
How can I use GSON/Jackson to make this sort of mapping automatic? I would like to only extract the entities values and make a list out of them.
EntityDto example: nca_location would be the name, and the list would be the parsed objects.
ResponseEntity response = restTemplate.exchange(requestUrl, HttpMethod.POST, entity, Object.class);
This is how I get the response object from the API and I have no idea how to proceed.

Related

Find values from list with key value pair

I want to find the key value pair from a list. I want to put the values into a parameter from another method. I have a deserialzied map that contains the list called "InfoFields" here is my json string:
{
"operation": "get-id",
"payload": {
"ApplicationContext": {
"Context": "JORDFEIL",
"SenderId": "xxx",
"subContext": ""
},
"supportIssue": {
"InfoFields": [
{
"Key": "key1",
"Value": "value1"
},
{
"Key": "key2",
"Value": "value2"
},
{
"Key": "key3",
"Value": "value3"
}
],
}
},
"type": "~:CustomerInquiry",
}
Here is my wrapper class aswell
public class WebskjemaModel {
public class ApplicationContext {
public String Context;
public String SenderId;
public String subContext;
}
public String operation;
public Payload payload;
public String type;
public class InfoFields {
public String Key;
public String Value;
}
public class Payload {
public ApplicationContext ApplicationContext;
public SupportIssue supportIssue;
}
public class SupportIssue {
public List<InfoFields> InfoFields;
public String assignId;
public String businessObjectType;
public String comment;
public String contactDate;
public String contactName;
public String customerName;
public String customerNo;
public String docClass;
public String format;
public String objectDescription;
public String objectId;
public String subject;
}
public static WebskjemaModel parse(String json) {
return (WebskjemaModel) System.JSON.deserialize(json, WebskjemaModel.class);
}
}
I have created a map that contains the deserialzed object, now i want to map the fields for a single case. Here is what i have tried, but i cant seem to get the values from the key and from the value:
public with sharing class WebskjemaCaseCreator {
#TestVisible
private Map<Id, Case> cases { get; set; }
#TestVisible
private Map<Id, WebskjemaModel> webskjemaModels = new Map<Id, WebskjemaModel>();
public WebskjemaCaseCreator(Map<Id, Case> cases) {
this.cases = cases;
deserializeJson();
mapFields();
}
private void deserializeJson() {
for (Id caseId : cases.keySet()) {
Case c = cases.get(caseId);
WebskjemaModel model = WebskjemaModel.parse(c.Webskjemablob__c);
webskjemaModels.put(caseId, model);
}
}
private void mapFields() {
for (Id caseId : cases.keySet()) {
Case c = cases.get(caseId);
WebskjemaModel model = webskjemaModels.get(caseId);
}
}
private void mapFieldsForSingleCase(Case c, WebskjemaModel model) {
String context = model.payload.ApplicationContext.Context;
List<WebskjemaModel.InfoFields> myinfoFields = model.payload.supportIssue.InfoFields;
for (WebskjemaModel.InfoFields fields : myinfoFields) {
mapInfoField(c, context, fields.get(key), fields.get(value));
}
// TODO: Find WebskjemaModel inforfields and loop
// for (list of infofields for this model only){
// TODO: call method mapInfoField
// }
}
private void mapInfoField(Case c, String context, String infoFieldKey, String infoFieldValue) {
WebskjemaMapping__mdt[] webskjemarecords = [
SELECT CaseField__c, Context__c, JsonField__c, IfsField__c
FROM WebskjemaMapping__mdt
];
// TODO: Find Case field from custom meta data type mapping, based on context and infoFielfKey
// TODO: Put value from inforFieldValue into Case field
}
}

Deserialization using Jackson Json where one of the keys is dynamic

I am fairly new to Jackson. I am trying to map the following json to a POJO using Jackson for deserialization.
{
"data": [
{
"customerName": "abc",
"varaible_Key1": {
"p1": "text data",
"p2": "textarea data",
........
}
},
{
"customerName": "bbc",
"varaible_Key2": {
"p1": "text",
"p2": "textarea"
......
}
},
{
"customerName": "xyz",
"varaible_Key3": {
"p1": "xyz text",
"p2": "xyz textarea"
......
}
}
///////more customername / variable_keys
]
}
The problem I am facing is with dynamic / variable keys in the json.
I have tried using #JsonAnySetter in the POJO as shown below.
public class Foo {
#JsonProperty("customerName")
private String name;
private Map<String, DataObject> properties;
#JsonAnyGetter
public Map<String, DataObject> getProperties() {
return properties;
}
#JsonAnySetter
public void add(String key, DataObject value) {
properties.put(key, value);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
where DataObject contains the fields like p1,p2 and so on.
public class DataObject {
#JsonProperty("p1")
private String firstValue;
#JsonProperty("p2")
private String secondValue;
#JsonProperty("p3")
private String thirdValue;
#JsonProperty("p4")
private String fourthValue;
public String getFirstValue() {
return firstValue;
}
public void setFirstValue(String firstValue) {
this.firstValue = firstValue;
}
public String getSecondValue() {
return secondValue;
}
public void setSecondValue(String secondValue) {
this.secondValue = secondValue;
}
public String getThirdValue() {
return thirdValue;
}
public void setThirdValue(String thirdValue) {
this.thirdValue = thirdValue;
}
public String getFourthValue() {
return fourthValue;
}
public void setFourthValue(String fourthValue) {
this.fourthValue = fourthValue;
}
}
I keep getting the below error. Any help on this is appreciated.
com.fasterxml.jackson.databind.JsonMappingException: N/A (through
reference chain:
com.epic.customer.dto.DataField["data"]->java.util.ArrayList[0]-> com.epic.customer.dto.Foo["varaible_Key1"])
at
com.fasterxml.jackson.databind.deser.SettableAnyProperty._throwAsIOE(SettableAnyProperty.java:214)
at
com.fasterxml.jackson.databind.deser.SettableAnyProperty.set(SettableAnyProperty.java:179)
at
com.fasterxml.jackson.databind.deser.SettableAnyProperty.deserializeAndSet(SettableAnyProperty.java:134)
at
com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1539)
at
com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:293)
at
com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at
com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:285)
at
com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:244)
at
com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
at
com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
at
com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:287)
at
com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at
com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
You just need to create 2 setters, for specific property:
class Foo {
private DataObject dataObject;
public DataObject getDataObject() {
return dataObject;
}
public void setVaraible_Key1(DataObject dataObject) {
this.dataObject = dataObject;
}
public void setVaraible_Key2(DataObject dataObject) {
this.dataObject = dataObject;
}
}
Further usages refers here.
I think you have to give it a hint on the type of object you want back:
ObjectMapper mapper = new ObjectMapper();
Map<String, DataObject> test = mapper.readValue("Insert Data in here", mapper.getTypeFactory().constructMapLikeType(HashMap.class, String.class, DataObject.class));

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 5921 path $.data[5].courier.data

I have this model object Courier :
public class Courier {
#SerializedName("data")
private List<User> data = null;
public Courier() {
}
public Courier(List<User> data) {
this.data = data;
}
public List<User> getData() {
return data;
}
public void setData(List<User> data) {
this.data = data;
}
}
I get this response from server:
{
"data": [
{
"id": 446,
"courier": {
"data": []
},
"title": "гром",
"description": "Логойский тракт 24 в России в начале следующей",
"departure": "ChIJPQKUckNv2UYRLr1NasgXZ08",
"arrival": "EkHQodC10YDQtdCx0YDRj9C90YvQuSDQv9C10YDQtdGD0LvQvtC6LCDQnNC-0YHQutCy0LAsINCg0L7RgdGB0LjRjw"
},
{
"id": 438,
"courier": {
"data": []
},
"title": "тест",
"description": "гппг лмш ш ш ш ш г У меня на сковородке стоит ли брать сва в кино мы все равно обсуждаем",
"departure": "ChIJH10nmDnP20YR-n7Kq6Whd5w",
"arrival": "Ej_QnNC-0YHQutCy0L7RgNC10YbQutCw0Y8g0YPQu9C40YbQsCwg0JzQvtGB0LrQstCwLCDQoNC-0YHRgdC40Y8"
},
{
"id": 439,
"courier": {
"data": []
},
"title": "лаьаьаат",
"description": "лала слат алс ал ала ал кща да аьад",
"departure": "ChIJH7D4cTnP20YRKlzSCnP6Mak",
"arrival": "Ej_QnNC-0YHQutCy0L7RgNC10YbQutCw0Y8g0YPQu9C40YbQsCwg0JzQvtGB0LrQstCwLCDQoNC-0YHRgdC40Y8"
},
{
"id": 442,
"courier": {
"data": {
"id": 122,
"email": null,
"phone": "73339999999",
"photo": null,
"rating": 0
}
},
"title": "картошечка",
"description": "Крупная сортированная",
"departure": "ChIJnZRv1jnP20YRWiezW55d1tA",
"arrival": "ChIJpfH6UJtp1EYRlhM20g-jzF4"
}
]
}
If object courier not have data, i get array "data": [], if object courier has data, i get object :
"courier": {
"data": {
"id": 122,
"email": null,
"phone": "73339999999",
"photo": null,
"rating": 0
}
}
And then I get error... Please give me advice how handle this case in android application...
is one of the most common mistakes when you start to use JSON with a client, for android please refer to this tutorial to understand
the best source to understand this kind of mistake is to read this post
a canonical SO post.
Is better to read it and understand it, that asking for a simple solution because you will go really often into this error.
while deserializing, Gson was expecting a JSON object, but found a
JSON array
A JSON Object is wrapped by a {
A JSON Array is wrapped by a [
What you need is to adapt your class Courier, to deserialize in the right way the JSON response.
take in mind that; a JSON array become deserialized in java as a Collection type or an array type;
PLEASE notice that is confusing to use two times data
on top of everything, the first data is
public class MyPojo
{
private Data[] data;
public Data[] getData ()
{
return data;
}
public void setData (Data[] data)
{
this.data = data;
}
#Override
public String toString()
{
return "ClassPojo [data = "+data+"]";
}
}
Data.class
public class Data
{
private String id;
private String title;
private String description;
private Courier courier;
private String arrival;
private String departure;
public String getId ()
{
return id;
}
public void setId (String id)
{
this.id = id;
}
public String getTitle ()
{
return title;
}
public void setTitle (String title)
{
this.title = title;
}
public String getDescription ()
{
return description;
}
public void setDescription (String description)
{
this.description = description;
}
public Courier getCourier ()
{
return courier;
}
public void setCourier (Courier courier)
{
this.courier = courier;
}
public String getArrival ()
{
return arrival;
}
public void setArrival (String arrival)
{
this.arrival = arrival;
}
public String getDeparture ()
{
return departure;
}
public void setDeparture (String departure)
{
this.departure = departure;
}
#Override
public String toString()
{
return "ClassPojo [id = "+id+", title = "+title+", description = "+description+", courier = "+courier+", arrival = "+arrival+", departure = "+departure+"]";
}
}
Courier.class
public class Courier
{
private String[] data;
public String[] getData ()
{
return data;
}
public void setData (String[] data)
{
this.data = data;
}
#Override
public String toString()
{
return "ClassPojo [data = "+data+"]";
}
}
I suggest you just to create a class Data with fields id, email, etc. And make field Data data in the class Courier instead of a List<> data
EDIT: then you can use a JsonDeserializer. Just remove #SerializedName("data") over the Data field, so that the Json will not parse this field. Then create a class:
public class CourierDeserializer implements JsonDeserializer<Courier> {
#Override
public Courier deserialize(final JsonElement json, final Type type,
final JsonDeserializationContext context) {
Courier result = new Gson().fromJson(json, Courier.class);
try {
if (json != null) {
result.setData((Data) context.deserialize(json, Data.class));
}
} catch (JsonParseException e) {
result.setData(null);
}
return result;
}
}
and finally register it where you create your GsonBuilder:
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapter(Courier.class, new CourierDeserializer());
mGson = gson.create();
builder.setConverter(new GsonConverter(mGson));
if you use Retrofit.

Dynamically add property:value in JSON through rest services

How do we create a json property:value dynamically by taking them as inputs from the user through a REST service, without having to have fields to hold the value.
Basically, the present output is :
{
"name": "X",
"price": "X",
"ticker": "X",
"status": "X",
"supplier": "X",
"attribute": [{
"key": "index",
"value": "Nasdaq"
},
{
"key": "priority",
"value": "high"
}
]
}
Required output is :
{
"name": "X",
"price": "X",
"ticker": "X",
"status": "X",
"supplier": "X",
"attribute": [{
"index": "Nasdaq"
},
{
"priority": "high"
}
]
}
Here, index and Nasdaq are the values given by the user, which is the key-value pair that is to be added to the attribute list.
The two POJO's used :
Stock.java
#XmlRootElement
public class Stock {
public Stock() {
}
public Stock(String name, double price, String ticker, String status, String supplier, List<KV> attribute) {
super();
this.name = name;
this.price = price;
this.ticker = ticker;
this.status = status;
this.supplier = supplier;
this.attribute = attribute;
}
private String name;
private double price;
private String ticker;
private String status;
private String supplier;
private List<KV> attribute;
public List<KV> getAttribute() {
return attribute;
}
public void setAttribute(List<KV> attribute) {
this.attribute = attribute;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getTicker() {
return ticker;
}
public void setTicker(String ticker) {
this.ticker = ticker;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getSupplier() {
return supplier;
}
public void setSupplier(String supplier) {
this.supplier = supplier;
}
}
And :
KV.java
public class KV {
private String key;
private String value;
public KV(String key, String value) {
super();
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Would prefer answers relating to usage of annotations or changes in the rest service, and not text processing at the end.
Okay, once you get the response you are getting currently then instead of a class "public class KV {} ", you can create a Hashmap,
& key of the Hashmap can hold the index/priority etc & value can hold Nasdaq/high.
Also name the Hashmap variable as "attribute".
So that you can extract the data from API using your current structure & then can convert it to one as I said above & send the response.
Try using Attributes class in java
Instead of defining a list of object you can just define
Attribute attributes;
For more details about how Attribute class works visit https://docs.oracle.com/javase/8/docs/api/org/xml/sax/Attributes.html

Map JSON to Java object

I have these type of JSON object which I'm getting from gridx filter expression:
{
"op": "or",
"data": [
{
"op": "contain",
"data": [
{
"op": "string",
"data": "id",
"isCol": true
},
{
"op": "string",
"data": "sdfv"
}
]
},
{
"op": "contain",
"data": [
{
"op": "string",
"data": "post",
"isCol": true
},
{
"op": "string",
"data": "sdfv"
}
]
},
{
"op": "contain",
"data": [
{
"op": "string",
"data": "birthday",
"isCol": true
},
{
"op": "string",
"data": "sdfv"
}
]
}
]
}
How I can map this to a Java object and then deserialize using Gson?
I've made these two classes:
package dto.Filter;
public class FilterData extends FilterExpression {
private String op;
private boolean isCol;
private String data;
public String getOp() {
return op;
}
public void setOp(String op) {
this.op = op;
}
public boolean isCol() {
return isCol;
}
public void setCol(boolean col) {
isCol = col;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
package dto.Filter;
import java.util.List;
public class FilterExpression {
private List<FilterData> filters;
private String op;
public List<FilterData> getFilters() {
return filters;
}
public void setFilters(List<FilterData> filters) {
this.filters = filters;
}
public String getOp() {
return op;
}
public void setOp(String op) {
this.op = op;
}
}
The problem is that I have both data as Object and String type. Do I need to use my custom TypeAdapter?
Make your data structure as
class DataStructure {
private String op;
private String data;
private String isCol;
public DataStructure(){
op="";
data="";
isCol="";
}
public String getOp() {
return op;
}
public void setOp(String op) {
this.op = op;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String isCol() {
return isCol;
}
public void setCol(String isCol) {
this.isCol = isCol;
}
#Override
public String toString() {
return "DataStructure [op=" + op + ", data=" + data + ", isCol=" + isCol + "]";
}
}
I have parse the JSON file using google GSON library. Here is maven repository.
Note: to read using GSON library add '[' at starting and ']' at ending.
I have read the JSON file and store the data in ArrayList. Hope after getting array list you can do serialization.
public class FilterData {
private static Gson gson = new Gson();
private static JsonParser parser = new JsonParser();
public static List<DataStructure> getData(List<DataStructure> datas){
List<DataStructure> result = new ArrayList<>();
for (DataStructure data : datas) {
for (JsonElement object : parser.parse(data.getData()).getAsJsonArray()) {
DataStructure dataStructure = new DataStructure();
JsonObject jObject = gson.fromJson(object, JsonObject.class);
dataStructure.setOp(jObject.get("op").toString());
dataStructure.setData(jObject.get("data").toString());
if (jObject.has("isCol"))
dataStructure.setData(jObject.get("isCol").toString());
System.out.println(dataStructure);
result.add(dataStructure);
}
}
return result;
}
public static void main(String[] args) throws FileNotFoundException {
JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream("input.json")));
List<DataStructure> datas = new ArrayList<>();
JsonArray jArray = parser.parse(reader).getAsJsonArray();
for (JsonElement object : jArray) {
DataStructure dataStructure = new DataStructure();
JsonObject jObject = gson.fromJson(object, JsonObject.class);
dataStructure.setOp(jObject.get("op").toString());
dataStructure.setData(jObject.get("data").toString());
if (jObject.has("isCol"))
dataStructure.setData(jObject.get("isCol").toString());
System.out.println(dataStructure);
datas.add(dataStructure);
}
List<DataStructure> insideData = getData(datas);
List<DataStructure> inside2Data = getData(insideData);
}
}

Categories

Resources