I have a json response like below:
{
"entry": {
"isFile": true,
"createdByUser": {
"id": "admin",
"displayName": "Administrator"
},
"modifiedAt": "2020-10-05T14:19:47.851+0000",
"nodeType": "hr:HR_Type",
"content": {
"mimeType": "application/pdf",
"mimeTypeName": "Adobe PDF Document",
"sizeInBytes": 151693,
"encoding": "UTF-8"
},
"parentId": "f73f912a-0b52-425b-92a1-14bef979fd63",
"aspectNames": [
"rn:renditioned",
"cm:versionable",
"cm:titled",
"cm:auditable",
"cm:author",
"cm:thumbnailModification"
],
"createdAt": "2020-10-05T13:59:20.378+0000",
"isFolder": false,
"modifiedByUser": {
"id": "admin",
"displayName": "Administrator"
},
"name": "YYYYest11qq890.pdf",
"id": "b850acd4-f68b-4149-b792-ffb339b680ed",
"properties": {
"hr:candidate_type": "EMP",
"cm:versionType": "MINOR",
"cm:versionLabel": "1.5",
"hr:ret_length": "10 years",
"cm:author": "Ajay Kumar",
"cm:lastThumbnailModification": [
"doclib:1601907587850"
]
}
}
}
I need to parse isFile .... all the way upto doclib capturing all the key value pairs.
Since I have varying response depending upon nodes of my source system, I have tried all of these:
But none of above gave me what I need.
I tried using gson (seems promising) and tried this:
String jsonString = "{....}";
JsonObject jsonObject = JsonParser.parseString(jsonString).getAsJsonObject();
for(Map.Entry<String, JsonElement> currency: jsonObject.getAsJsonObject("entry").entrySet()){
System.out.println("The value for: "+ currency.getKey()+" --- is: " + currency.getValue());
}
Above gives me:
The value for: isFile --- is: true
The value for: createdByUser --- is: {"id":"admin","displayName":"Administrator"}
The value for: modifiedAt --- is: "2020-10-05T14:19:47.851+0000"
The value for: nodeType --- is: "hr:HR_Type"
The value for: content --- is: {"mimeType":"application/pdf","mimeTypeName":"Adobe PDF Document","sizeInBytes":151693,"encoding":"UTF-8"}
The value for: parentId --- is: "f73f912a-0b52-425b-92a1-14bef979fd63"
The value for: aspectNames --- is: ["rn:renditioned","cm:versionable","cm:titled","cm:auditable","cm:author","cm:thumbnailModification"]
The value for: createdAt --- is: "2020-10-05T13:59:20.378+0000"
The value for: isFolder --- is: false
The value for: modifiedByUser --- is: {"id":"admin","displayName":"Administrator"}
The value for: name --- is: "YYYYest11qq890.pdf"
The value for: id --- is: "b850acd4-f68b-4149-b792-ffb339b680ed"
The value for: properties --- is: {"hr:candidate_type":"EMP","cm:versionType":"MINOR","cm:versionLabel":"1.5","hr:ret_length":"10 years","cm:author":"Ajay Kumar","cm:lastThumbnailModification":["doclib:1601907587850"]}
Question 1: Now how do I access/extract the set of key values for (say) properties? This section:
"properties": {
"hr:candidate_type": "EMP",
"cm:versionType": "MINOR",
"cm:versionLabel": "1.5",
"hr:ret_length": "10 years",
"cm:author": "Ajay Kumar",
"cm:lastThumbnailModification": [
"doclib:1601907587850"
]
}
I am clueless here. And want to use the same approach because I dont know what json string I am going to get and hence I can not use a bean to map the values. I want to capture all key+value pairs dynamically. Any pointers/help/solution will be greatly appreciated.
Update:
The ugly way I am using to extract each node is similar to this:
String theId = jsonObject.getAsJsonObject("entry").get("id").getAsString();
System.out.println("\n\nId :: "+ theId + "\n\n");
String isFile = jsonObject.getAsJsonObject("entry").get("isFile").getAsString();
System.out.println("\n\nisFile :: "+ isFile + "\n\n");
for(Map.Entry<String, JsonElement> entry: jsonObject.getAsJsonObject("entry").getAsJsonObject("createdByUser").entrySet()){
System.out.println("The value for createdByUser: "+ entry.getKey()+" --- is: " + entry.getValue());
}
String modifiedAt = jsonObject.getAsJsonObject("entry").get("modifiedAt").getAsString();
System.out.println("\n\nmodifiedAt :: "+ modifiedAt + "\n\n");
String nodeType = jsonObject.getAsJsonObject("entry").get("nodeType").getAsString();
System.out.println("\n\nnodeType :: "+ nodeType + "\n\n");
for(Map.Entry<String, JsonElement> entry: jsonObject.getAsJsonObject("entry").getAsJsonObject("content").entrySet()){
System.out.println("The value for content: "+ entry.getKey()+" --- is: " + entry.getValue());
}
String parentId = jsonObject.getAsJsonObject("entry").get("parentId").getAsString();
System.out.println("\n\nparentId :: "+ parentId + "\n\n");
for(JsonElement entry: jsonObject.getAsJsonObject("entry").getAsJsonArray("aspectNames")){
System.out.println("aspectNames: "+ entry);
}
String createdAt = jsonObject.getAsJsonObject("entry").get("createdAt").getAsString();
System.out.println("\n\ncreatedAt :: "+ createdAt + "\n\n");
String isFolder = jsonObject.getAsJsonObject("entry").get("isFolder").getAsString();
System.out.println("\n\nisFolder :: "+ isFolder + "\n\n");
for(Map.Entry<String, JsonElement> entry: jsonObject.getAsJsonObject("entry").getAsJsonObject("modifiedByUser").entrySet()){
System.out.println("The value for modifiedByUser: "+ entry.getKey()+" --- is: " + entry.getValue());
}
String name = jsonObject.getAsJsonObject("entry").get("name").getAsString();
System.out.println("\n\nname :: "+ name + "\n\n");
String id = jsonObject.getAsJsonObject("entry").get("id").getAsString();
System.out.println("\n\nid :: "+ id + "\n\n");
for(Map.Entry<String, JsonElement> entry: jsonObject.getAsJsonObject("entry").getAsJsonObject("properties").entrySet()){
System.out.println("The value for properties: "+ entry.getKey()+" --- is: " + entry.getValue());
}
Question 2: Is this the only way we extract key value pair using gson? Or there is some correct/cleaner way to achieve it? If yes, please show some direction.