how to deserialize a json object to collection using jackson - java

I want to deserialize a json like this
{
"0":{"name":"Alice"},
"1":{"name":"Bob"}
}
to a java collection( set or list, not map).
I want to change the default behavior of CollectionDeserializer to support this and config it as a global configuration. Any way to to this?

If you really have this structure (an object as a container and not an array, which could be handled much easier):
import com.fasterxml.jackson.databind.*;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
public class Main {
public static void main(String[] args) throws IOException {
String json = "{\"0\":{\"name\":\"Alice\"}, \"1\":{\"name\":\"Bob\"}}";
ObjectMapper mapper = new ObjectMapper();
JsonNode obj = mapper.readValue(json, JsonNode.class);
Iterator<Map.Entry<String, JsonNode>> userEntries = obj.fields();
while(userEntries.hasNext()){
Map.Entry<String, JsonNode> userEntry = userEntries.next();
System.out.println(userEntry.getKey() + " => " + userEntry.getValue());
}
}
}

You can achieve this task using gson api.
The code is as follows:
String yourJson = "{\"0\":{\"name\":\"Alice\"}, \"1\":{\"name\":\"Bob\"}}";
Gson gson = new Gson();
Type tarType = new TypeToken<Map<String,Map<String,String>>>(){
}.getType();
gson.fromJson(yourJson, tarType);
For this you need to add following:
com.google.gson.Gson

Why not to turn your JSON document into an array as:
{"persons":[{"name":"Alice"},{"name":"Bob"}]}
Then define a corresponding JSON schema (assuming PersonArray is the file name):
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Some description",
"type" : "object",
"properties" : {
"persons" : {
"type" : "array",
"items" : { "$ref": "#/definitions/person" }
}
},
"definitions": {
"person" : {
"type": "object",
"description": "A person",
"properties": {
"name": { "type": "string" }
}
}
}
}
and take advantage of Jackson Data Binding API by using jsonschema2pojo-maven-plugin Maven plugin to generate POJOs in Java (alternatively, you can manually implement POJOs).
Once POJOs are generated, you can use ObjectMapper to deserialise JSON document in the following way:
ObjectMapper mapper = new ObjectMapper();
PersonArray personArray = mapper.readValue(serialisedJsonDocument, PersonArray.class);
The elements of your JSON document will be stored inside PersonArray object as:
List<Person> persons = new ArrayList<Person>();
You could also add additional properties to the Person object if you needed.

Related

Json Parsing: How to fetch "body" data inside "body array" using Gson (or better in other java library or notation supported)?

My Rest API is returning the following response, in which only the inner list is required, all data shall be discarded:
{
"meta": [],
"links": [],
"body": [
{
"meta": [],
"links": [],
"body": {
"field1": "value1",
"fieldn": "valuen"
} // <-----
},
{
"meta": [],
"links": [],
"body": {
"field1": "value1",
"fieldn": "valuen"
} // <-----
}
]
}
Is there any way in Gson or another other java library to fetch an array of the body or a straightforward way of doing that? Or maybe even using standard of java 8?
Or, should I use a standard iterator as follows:
//Old way to do this
JSONArray BodyArr = (JSONArray) jsonObject.get("Body");
Iterator<JSONObject> itBody = BodyArr.iterator();
int teller = 0;
while (itBody.hasNext()) {
JSONObject bodyObj = itBody.next();
JSONObject body = (JSONObject) bodyObj.get("Body");
}
Also in mysql we have way to do that using notation ($.body.body[] etc.). Is there any notational way to fetch the object
I think we have a nicely written article on this.
Json object iteration
If you have a class that represents an object in the array, then you can deserialize the JSONArray to an array of that class using public <T> T fromJson​(JsonElement json, java.lang.Class<T> classOfT) throws JsonSyntaxException on the Gson class:
class BodyItem {
public String[] meta;
public String[] links;
public String field1;
public String fieldn;
}
public BodyItem[] getBodyItems(final Gson gson, final JsonObject jsonObject) {
final JsonElement body = jsonObject.get("body");
return gson.fromJson(body, BodyItem[].class);
}
public static void main(final String[] args) {
final String response = "<your REST API JSON response>";
final Gson gson = new Gson();
final JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
final BodyItem[] bodyItems = getBodyItems(gson, jsonObject);
}
If you want a more notational way of accessing fields in Gson objects, you can use JsonObject's convenience accessors:
JsonArray getAsJsonArray​(java.lang.String memberName)
JsonObject getAsJsonObject​(java.lang.String memberName)
JsonPrimitive getAsJsonPrimitive(java.lang.String memberName)
And then with a JsonArray, you can iterate with for (final JsonElement element : jsonArray) or .forEach, and you can get JsonElements with the JsonElement get(int i) accessor.
So, say you had your original JsonObject response and wanted to get the value of body.field1 in the second element of the body list, you might do:
String value = jsonObject
.getAsJsonArray("body")
.get(1)
.getAsJsonObject()
.getAsJsonObject("body")
.getAsJsonObject("field1");

apply gson UPPER_CAMEL_CASE for specific sub-json

I have these messages arriving from SQS:
{
"eventID": "zzz",
"eventName": "MODIFY",
"eventVersion": "1.1",
"eventSource": "aws:dynamodb",
"awsRegion": "us-east-1",
"dynamodb": {
"ApproximateCreationDateTime": 1521976320,
"Keys": {
"key_1": {
"S": "yyy"
},
"key_2": {
"S": "xxx"
}
},
"SequenceNumber": "123",
"SizeBytes": 321,
"StreamViewType": "KEYS_ONLY"
},
"eventSourceARN": "arn:aws:dynamodb:us-east-1:eventSourceARN",
"itemType": "myItem"
}
I want to use gson library to convert this json string into a Record object (com.amazonaws.services.dynamodbv2.model.Record) which contains a StreamRecord object (com.amazonaws.services.dynamodbv2.model.StreamRecord) that represents the dynamodb sub json.
problem is that the inner fields of the dynamodb object are PascalCase while the other fields are normal camelCase.
This code:
Gson gson = new GsonBuilder()
//.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.create();
String json = <the json from the example above>
Record record = gson.fromJson(json, Record.class);
log.info("record="+record.toString());
StreamRecord dynamodb = record.getDynamodb();
log.info("dynamodb="+dynamodb.toString());
Map<String, AttributeValue> keys = dynamodb.getKeys();
log.info("keys="+keys.toString());
prints this log (UPPER_CAMEL_CASE commented out) :
record={EventID: zzz,EventName: MODIFY,EventVersion: 1.1,EventSource: aws:dynamodb,AwsRegion: us-east-1,Dynamodb: {},}
and then throws Null Pointer exception because the dynamoDb object is empty - because my json string is UPPER_CAMEL_CASE, while in the object its normal camelCase.
I want to apply FieldNamingPolicy.UPPER_CAMEL_CASE only for the dynamodb sub json.
perhaps somehow using FieldNamingStrategy ?
The json is given and I cannot change its schema.
I also can't change the fact that I get it as string.
see AWS API:
https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_Record.html
https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_StreamRecord.html
You seem to want the following naming strategy:
private static final Gson gson = new GsonBuilder()
.setFieldNamingStrategy(field -> {
if ( field.getDeclaringClass() == StreamRecord.class ) {
return FieldNamingPolicy.UPPER_CAMEL_CASE.translateName(field);
}
return FieldNamingPolicy.IDENTITY.translateName(field);
})
.create();
I usually never use naming strategies in favor of the #SerializedName annotation though, just to be more precise when declaring mappings.

How to parse Json with following structure

[
{
"updated_at":"2012-03-02 21:06:01",
"fetched_at":"2012-03-02 21:28:37.728840",
"description":null,
"language":null,
"title":"JOHN",
"url":"http://rus.JOHN.JOHN/rss.php",
"icon_url":null,
"logo_url":null,
"id":"4f4791da203d0c2d76000035",
"modified":"2012-03-02 23:28:58.840076"
},
{
"updated_at":"2012-03-02 14:07:44",
"fetched_at":"2012-03-02 21:28:37.033108",
"description":null,
"language":null,
"title":"PETER",
"url":"http://PETER.PETER.lv/rss.php",
"icon_url":null,
"logo_url":null,
"id":"4f476f61203d0c2d89000253",
"modified":"2012-03-02 23:28:57.928001"
}
]
First create one class (Json_obj) for your json data.Then you can try this :
String json_str='[ { "updated_at":"2012-03-02 21:06:01", "fetched_at":"2012-03-02 21:28:37.728840", "description":null, "language":null, "title":"JOHN", "url":"http://rus.JOHN.JOHN/rss.php", "icon_url":null, "logo_url":null, "id":"4f4791da203d0c2d76000035", "modified":"2012-03-02 23:28:58.840076" }, { "updated_at":"2012-03-02 14:07:44", "fetched_at":"2012-03-02 21:28:37.033108", "description":null, "language":null, "title":"PETER", "url":"http://PETER.PETER.lv/rss.php", "icon_url":null, "logo_url":null, "id":"4f476f61203d0c2d89000253", "modified":"2012-03-02 23:28:57.928001" } ]';
Gson gson = new Gson();
Json_obj json_obj = gson.fromJson(json_str, Json_obj.class);
Now your json data is converted in an object.You can fetch any value from that object.
I suggest you just use jackson library instead.
You can just quickly have
ObjectMapper mapper = new ObjectMapper () // this reads json to Pojo and writes Pojo to json
YourPojoClass obj = mapper.readValue (....)
Reference: mkyong.com/java/jackson-2-convert-java-object-to-from-json

Gson deserialize json of embedded member

I have the following sample JSON:
{
"birds":[
{
"id":"SAMPLEID",
"isTest":true,
"externalId":{
"Main":[
"123ABC"
],
"Sub":[
"456"
]
},
"dinos":[
],
"rhinos":[
{
"id":"SUPER100ID",
"isTest":true,
"externalId":{
"famousId":[
"23|45"
]
},
"dinos":[
],
"pelicans":[
{
"id":"D4CLIK",
"isTest":true,
"bird":null,
"crazyArray":[
]
},
{
"id":"DID123",
"type":"B",
"name":"TONIE",
"isTest":true,
"bird":null,
"subspecies":[
]
}
]
}
]
}
],
"metaData":{
"count":1
}
}
I want to use GSON to deserialize this JSON String and get the value of "famousId" member only.
I have looked through other answers and it seems that I will absolutely need to create classes for this.
Would it be possible to deserialize this without mapping POJOs, using JsonParser, JsonElement, JsonArray, etc? I have tried several permutations of this but with no success.
I also have tried the following code but it is also not working as expected:
JsonObject o = new JsonParser().parse(jsonResponseString).getAsJsonObject();
Gson gson = new Gson();
enterprises ent = new enterprises();
ent = gson.fromJson(o, enterprises.class);
#Getter
#Setter
class birds {
#JsonProperty("rhinos")
List<Rhino> rhinos = new ArrayList<Rhino>();
}
#Getter
#Setter
class Rhino {
#JsonProperty("externalId")
ExternalId externalId;
}
#Getter
#Setter
#JsonPropertyOrder({
"famousId"
})
class ExternalId {
#JsonProperty("famousId")
List<String> famousId = new ArrayList<String>();
}
Unfortunately this does not work either, so I guess a two part question...is it possible to simply deserialize and get the String value for famousId that I want, and what is incorrect with my current class structure?
You've almost done. I added a root class(Enterprises) for your json structure.
class Enterprises {
List<Birds> birds;
}
class Birds {
List<Rhino> rhinos;
}
class Rhino {
ExternalId externalId;
}
class ExternalId {
List<String> famousId;
}
Run below code:
JsonObject o = new JsonParser().parse(jsonResponseString).getAsJsonObject();
Gson gson = new Gson();
Enterprises enterprises = gson.fromJson(o, Enterprises.class);
System.out.println("famousId:" + enterprises.birds.get(0).rhinos.get(0).externalId.famousId.get(0));
Output:
famousId:23|45
Or if you don't want to use pojo classes:
JsonObject o = new JsonParser().parse(jsonResponseString).getAsJsonObject();
JsonArray birdsJsonArray = (JsonArray) o.get("birds");
JsonArray rhinosJsonArray = (JsonArray)((JsonObject)(birdsJsonArray.get(0))).get("rhinos");
JsonObject externalIdJsonObject = (JsonObject)((JsonObject)(rhinosJsonArray.get(0))).get("externalId");
JsonArray famousIdJsonArray = (JsonArray)externalIdJsonObject.get("famousId");
System.out.println("famousId:" + famousIdJsonArray.get(0));
Output:
famousId:23|45

Nested Json to Map using Jackson

I'm trying to dynamically parse some JSON to a Map. The following works well with simple JSON
String easyString = "{\"name\":\"mkyong\", \"age\":\"29\"}";
Map<String,String> map = new HashMap<String,String>();
ObjectMapper mapper = new ObjectMapper();
map = mapper.readValue(easyString,
new TypeReference<HashMap<String,String>>(){});
System.out.println(map);
But fails when I try to use some more complex JSON with nested information. I'm trying to parse the sample data from json.org
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": [
"GML",
"XML"
]
},
"GlossSee": "markup"
}
}
}
}
}
I get the following error
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
Is there a way to parse complex JSON data into a map?
I think the error occurs because the minute Jackson encounters the { character, it treats the remaining content as a new object, not a string. Try Object as map value instead of String.
public static void main(String[] args) throws IOException {
Map<String,String> map = new HashMap<String,String>();
ObjectMapper mapper = new ObjectMapper();
map = mapper.readValue(x, new TypeReference<HashMap>(){});
System.out.println(map);
}
output
{glossary={title=example glossary, GlossDiv={title=S, GlossList={GlossEntry={ID=SGML, SortAs=SGML, GlossTerm=Standard Generalized Markup Language, Acronym=SGML, Abbrev=ISO 8879:1986, GlossDef={para=A meta-markup language, used to create markup languages such as DocBook., GlossSeeAlso=[GML, XML]}, GlossSee=markup}}}}}
Wrap your Map into a dumb object as container, like this:
public class Country {
private final Map<String,Map<String,Set<String>>> citiesAndCounties=new HashMap<>;
// Generate getters and setters and see the magic happen.
}
The rest is just working with your Object mapper, example Object mapper using Joda module:
public static final ObjectMapper JSON_MAPPER=new ObjectMapper().
disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).
setSerializationInclusion(JsonInclude.Include.NON_NULL).
disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).
registerModule(new JodaModule());
// Calling your Object mapper
JSON_MAPPER.writeValueAsString(new Country());
Hope that helps ;-)

Categories