how to convert json to list/map with jackson - java

I encountered a piece of json as below:
{
"code": 1,
"msg": "query success",
"obj": {
"data": [
{
"name": "",
"value": {
"num": "89"
}
},
{
"name": "1",
"value": {
"num": "19"
}
},
{
"name": "2",
"value": {
"num": "6"
}
}
]
},
"success": true,
"duration": 523
}
I need name and num. How to convert it to a list of java.util.List<HashMap<String, String>> with Jackson?

You can use Jackson ObjectMapper with TypeReference ,
First you need to read it as Map<String,Object> then you can extract the name and num with Java.
you need to get the data from obj then for each item in the dataList you have to map it to a new HashMap as shown below
ObjectMapper objectMapper = new ObjectMapper ();
Map<String,Object> jsonMap = objectMapper.readValue(jsonData,new TypeReference<Map<String,Object>>(){});
List<Object> data = ((Map<String,Object>)jsonMap.get("obj")).get("data");
List<Map<String,String> result = data.stream()
.map(d->(Map<String,Object> d)
.map(d->{
Map<String,String> map = new HashMap();
map.put(d.get("name"),((Map<String,String>)d.get("value")).get("num"));
return map;
})
.collect(Collectors.toList());
However if you can create a class for the the data it will be bit easier.

Library Josson can do the job.
https://github.com/octomix/josson
Deserialization
Josson josson = Josson.fromJsonString(
"{" +
" \"code\": 1," +
" \"msg\": \"query success\"," +
" \"obj\": {" +
" \"data\": [" +
" {" +
" \"name\": \"\"," +
" \"value\": {" +
" \"num\": \"89\"" +
" }" +
" }," +
" {" +
" \"name\": \"1\"," +
" \"value\": {" +
" \"num\": \"19\"" +
" }" +
" }," +
" {" +
" \"name\": \"2\"," +
" \"value\": {" +
" \"num\": \"6\"" +
" }" +
" }" +
" ]" +
" }," +
" \"success\": true," +
" \"duration\": 523" +
"}");
Transformation
JsonNode node = josson.getNode("obj.data.map(name::value.num)");
System.out.println(node);
List<Map<String, String>> result = Josson.readValueFor(node, List.class);
System.out.println(result);
Output
[{"":"89"},{"1":"19"},{"2":"6"}]
[{=89}, {1=19}, {2=6}]

Related

Read below JSON data and fetch status name

I want to get the name of the status from JSON data.
{
"expand": "renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations",
"id": "3876562",
"self": "https://jira2.abc.com/rest/api/2/issue/3876562",
"key": "DEVACDMY-35289",
"fields": {
"status": {
"self": "https://jira2.abc.com/rest/api/2/status/3",
"description": "",
"iconUrl": "https://jira2.abc.com/images/icons/statuses/inprogress.png",
"name": "In Progress",
"id": "3",
"statusCategory": {
"self": "https://jira2.abc.com/rest/api/2/statuscategory/4",
"id": 4,
"key": "indeterminate",
"colorName": "yellow",
"name": "In Progress"
}
}
}
}
I try this but it doesn't work.
Map address = ((Map)jo.get("fields"));
// iterating address Map
Iterator<Map.Entry> itr1 = address.entrySet().iterator();
while (itr1.hasNext()) {
Map.Entry pair = itr1.next();
Map status=((Map)jo.get(pair.key);
Iterator<Map.Entry> itr2 =status.entrySet().iterator(); while(itr2.hasNext())
{
itr1=((Map)itr2.next()).entrySet().iterator();
while(itr1.hasNext())
{
Map.Entry pair=itr1.next();
System.out.println(pair.getKey()+":"+pair.getValue());
}}
Looking at the json, it is not an array, it is simply a JsonObject and to get the string of the status name you need to traverse the same way.
To get the status name there is a hierarchy that you need to follow
JSONObject --> fields*(this is again a JSONObject)* --> status*(this is
again a JSON object)* -> name (this is string)
Look at the following code
String str = "{\n" +
" \"expand\": \"renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations\",\n" +
" \"id\": \"3876562\",\n" +
" \"self\": \"https://jira2.abc.com/rest/api/2/issue/3876562\",\n" +
" \"key\": \"DEVACDMY-35289\",\n" +
" \"fields\": {\n" +
" \"status\": {\n" +
" \"self\": \"https://jira2.abc.com/rest/api/2/status/3\",\n" +
" \"description\": \"\",\n" +
" \"iconUrl\": \"https://jira2.abc.com/images/icons/statuses/inprogress.png\",\n" +
" \"name\": \"In Progress\",\n" +
" \"id\": \"3\",\n" +
" \"statusCategory\": {\n" +
" \"self\": \"https://jira2.abc.com/rest/api/2/statuscategory/4\",\n" +
" \"id\": 4,\n" +
" \"key\": \"indeterminate\",\n" +
" \"colorName\": \"yellow\",\n" +
" \"name\": \"In Progress\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
JSONObject jsonarray = new JSONObject(str);
JSONObject fields = jsonarray.getJSONObject("fields");
JSONObject status = fields.getJSONObject("status");
String name = status.getString("name");
System.out.println("fields : " + fields);
System.out.println("status : " + status);
System.out.println("name: " + name);
output :-
fields : {"status":{"name":"In Progress","self":"https://jira2.abc.com/rest/api/2/status/3","description":"","iconUrl":"https://jira2.abc.com/images/icons/statuses/inprogress.png","id":"3","statusCategory":{"colorName":"yellow","name":"In Progress","self":"https://jira2.abc.com/rest/api/2/statuscategory/4","id":4,"key":"indeterminate"}}}
status : {"name":"In Progress","self":"https://jira2.abc.com/rest/api/2/status/3","description":"","iconUrl":"https://jira2.abc.com/images/icons/statuses/inprogress.png","id":"3","statusCategory":{"colorName":"yellow","name":"In Progress","self":"https://jira2.abc.com/rest/api/2/statuscategory/4","id":4,"key":"indeterminate"}}
name: In Progress

2 Different Json Objects are printed as a response, how to separate them and read the value of an element in the second Object

I am getting multiple json objects in a http response.
Eg:
{
"result": {
"Status": "complete",
"id1": "<id1>",
"id2": "<id2>",
"Fields": [{
"fid": "1",
"FName": "<name>"
}, {
"fid": "2",
"FName": "<name>"
}, {
"fid": "3",
"FName": "<name>"
}, {
"fid": "4",
"FName": "<name>"
}]
}
}
The next object is
{
"result": {
"Status": "complete",
"id1": "<id1>",
"id2": "<id2>",
"Fields": [{
"fid": "1",
"FName": "<name>"
}, {
"fid": "2",
"FName": "<name>"
}, {
"fid": "3",
"FName": "<name>"
}, {
"fid": "4",
"FName": "<name>"
}],
"TokenPairs": [{
"Token1": "<token1>",
"Token2": "<token2>",
"FieldMatches": {
"additionalProp1": {
"Score": 0,
"FalseDiscoveryRate": 0
},
"additionalProp2": {
"Score": 0,
"FalseDiscoveryRate": 0
},
"additionalProp3": {
"Score": 0,
"FalseDiscoveryRate": 0
}
}
}]
}
}
I actually want to check the TokenPairs but when I check the jsonPath it picks up the 1st object and returns null. How can I skip the 1st object and go to the second object? I m trying to solve it with adding the response (String) to JsonObject but am getting some errors.
I think I found a solution for you. I would like to provide you the following function which will give you two separate JSON String.
public void JSONSeparator(String JsonString) {
try {
JSONObject object1 = (JSONObject) new JSONTokener(jsonString.replaceAll("\\s+", "")).nextValue();
String firstJson = object1.toString().trim();
String nextJson = jsonString.replaceAll("\\s+", "").substring(firstJson.length());
System.out.println("First JSON String: " + firstJson);
System.out.println("Second JSON String: " + nextJson);
} catch (Exception e) {
e.printStackTrace();
}
}
The idea is to take the first JSON token, and then get the remaining part of the string after you get the first. I used the JSONTokener for picking up the first JSONObject. Then took the length of the object and hence I got the second JSON String using substring method.
The JsonString that I used to pass through this function was the following.
private String jsonString = "{\n" +
" \"result\": {\n" +
" \"Status\": \"complete\",\n" +
" \"id1\": \"<id1>\",\n" +
" \"id2\": \"<id2>\",\n" +
" \"Fields\": [{\n" +
" \"fid\": \"1\",\n" +
" \"FName\": \"<name>\"\n" +
" }, {\n" +
" \"fid\": \"2\",\n" +
" \"FName\": \"<name>\"\n" +
" }, {\n" +
" \"fid\": \"3\",\n" +
" \"FName\": \"<name>\"\n" +
" }, {\n" +
" \"fid\": \"4\",\n" +
" \"FName\": \"<name>\"\n" +
" }]\n" +
" }\n" +
"}" + "{" +
" \"result\": {\n" +
" \"Status\": \"complete\",\n" +
" \"id1\": \"<id1>\",\n" +
" \"id2\": \"<id2>\",\n" +
" \"Fields\": [{\n" +
" \"fid\": \"1\",\n" +
" \"FName\": \"<name>\"\n" +
" }, {\n" +
" \"fid\": \"2\",\n" +
" \"FName\": \"<name>\"\n" +
" }, {\n" +
" \"fid\": \"3\",\n" +
" \"FName\": \"<name>\"\n" +
" }, {\n" +
" \"fid\": \"4\",\n" +
" \"FName\": \"<name>\"\n" +
" }],\n" +
" \"TokenPairs\": [{\n" +
" \"Token1\": \"<token1>\",\n" +
" \"Token2\": \"<token2>\",\n" +
" \"FieldMatches\": {\n" +
" \"additionalProp1\": {\n" +
" \"Score\": 0,\n" +
" \"FalseDiscoveryRate\": 0\n" +
" },\n" +
" \"additionalProp2\": {\n" +
" \"Score\": 0,\n" +
" \"FalseDiscoveryRate\": 0\n" +
" },\n" +
" \"additionalProp3\": {\n" +
" \"Score\": 0,\n" +
" \"FalseDiscoveryRate\": 0\n" +
" }\n" +
" }\n" +
" }]\n" +
" }\n" +
"}";
Please note that, in order to get precise index for the substring, we need to remove all spaces and new lines using the replaceAll function that I had used. Hope that little trick helps you.

Json object, convert json array into a json object in java

I have one json array like this
"values":[
{
"locale":"en_US",
"source_key":"book_format",
"value":"Hardback",
"display_attr_name":"Book Format",
"source_value":"Hardback",
"isPrimary":"true"
},
{
"isFacetValue":"true",
"facet_version":"1.1",
"locale":"en_US",
"value":"Hardcover"
}
]
I need to get the only the distinct keys from above json array
{
"locale":"en_US",
"source_key":"book_format",
"value":"Hardback",
"display_attr_name":"Book Format",
"source_value":"Hardback",
"isPrimary":"true",
"isFacetValue":"true",
"facet_version":"1.1"
}
And the output will be in the form of jsonobject.
https://github.com/octomix/josson
Josson josson = Josson.fromJsonString(
"{" +
" \"values\": [" +
" {" +
" \"locale\": \"en_US\"," +
" \"source_key\": \"book_format\"," +
" \"value\": \"Hardback\"," +
" \"display_attr_name\": \"Book Format\"," +
" \"source_value\": \"Hardback\"," +
" \"isPrimary\": \"true\"" +
" }," +
" {" +
" \"isFacetValue\": \"true\"," +
" \"facet_version\": \"1.1\"," +
" \"locale\": \"en_US\"," +
" \"value\": \"Hardcover\"" +
" }" +
" ]" +
"}");
JsonNode node = josson.getNode("values.mergeObjects()");
System.out.println(node.toPrettyString());
Output
{
"locale" : "en_US",
"source_key" : "book_format",
"value" : "Hardcover",
"display_attr_name" : "Book Format",
"source_value" : "Hardback",
"isPrimary" : "true",
"isFacetValue" : "true",
"facet_version" : "1.1"
}

populating nested records with array in Avro using a GenericRecord

I have the following schema:
{
"name": "AgentRecommendationList",
"type": "record",
"fields": [
{
"name": "userid",
"type": "string"
},
{
"name": "friends",
"type": {
"type": "array",
"items": {
"name": "SchoolFriends",
"type": "record",
"fields": [
{
"name": "Name",
"type": "string"
},
{
"name": "phoneNumber",
"type": "string"
},
{
"name": "email",
"type": "string"
}
]
}
}
}
]
}
I'm using GenericRecord, and I want to put in an array of arrays for the SchoolFriends.
val avschema = new RestService(URL).getLatestVersion(name)
val schema = new Schema.Parser().parse(avschema.getSchema)
val record = new GenericData.Record(schema)
I would like to do something like record.put(x)
for that particular schema, you can do it in the following way. I would recommend put your record type SchoolFriends in a different schema, it would make easy to get the schema for the collection elements.
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericRecord;
import java.util.ArrayList;
import java.util.List;
public class PopulateNestedAvroObjects {
public static void main(String [] args ){
String strSchema = "{\n" +
" \"name\": \"AgentRecommendationList\",\n" +
" \"type\": \"record\",\n" +
" \"fields\": [\n" +
" {\n" +
" \"name\": \"userid\",\n" +
" \"type\": \"string\"\n" +
" },\n" +
" {\n" +
" \"name\": \"friends\",\n" +
" \"type\": {\n" +
" \"type\": \"array\",\n" +
" \"items\": {\n" +
" \"name\": \"SchoolFriends\",\n" +
" \"type\": \"record\",\n" +
" \"fields\": [\n" +
" {\n" +
" \"name\": \"Name\",\n" +
" \"type\": \"string\"\n" +
" },\n" +
" {\n" +
" \"name\": \"phoneNumber\",\n" +
" \"type\": \"string\"\n" +
" },\n" +
" {\n" +
" \"name\": \"email\",\n" +
" \"type\": \"string\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" }\n" +
" ]\n" +
"}";
Schema schema = new Schema.Parser().parse(strSchema);
GenericRecord record = new GenericData.Record(schema);
record.put("userid", "test user");
Schema childSchema = record.getSchema().getField("friends").schema().getElementType();
List<GenericRecord> friendList = new ArrayList();
GenericRecord friend1 = new GenericData.Record(childSchema);
friend1.put("Name", "1");
friend1.put("phoneNumber", "2");
friend1.put("email", "3");
friendList.add(friend1);
record.put("friends", friendList);
System.out.println(record);
}
}

To get the count of key-value occurrence for JSON in java

I have a json array as below:
[
{
"_id": {
"$oid": "57833bf8cb3099a383e8e2af"
},
"Name": "3GBWS",
"Version": "QV3.2",
"Type": "FQGA",
"SerialNO": "L1D73708884",
"Location": "TEXAS"
},
{
"_id": {
"$oid": "5784818bcb30b4918964b50f"
},
"Name": "3GBTW",
"Version": "WN6.0",
"Type": "FQGW",
"SerialNO": "O1143734584",
"Location": "OHIO"
},
{
"_id": {
"$oid": "5784818bcb30b4918964b50f"
},
"Name": "TEXAS",
"Version": "AS1.0",
"Type": "FWQA",
"SerialNO": "DH783708884",
"Location": "NY"
},
{
"_id": {
"$oid": "5784818bcb30b4918964b50f"
},
"Name": "3GLTS",
"Version": "WE9.0",
"Type": "FQGW",
"SerialNO": "L0943708884",
"Location": "TEXAS"
}
]
My aim is to get output as Texas=2 .
Here I need to get the occurrence of "TEXAS" only under the key "Location".
Is there any way to compare the key-value pair in java? I tried with int location = Collections.frequency(json_string, "TEXAS"); , but this wont consider the key "Location".
Please help.
Since you haven't provided your JSON library I'm assuming org.json.
import org.json.JSONArray;
import org.json.JSONObject;
public class Test {
public static void main(String... args) {
String json = "[\r\n" +
" {\r\n" +
" \"_id\": {\r\n" +
" \"$oid\": \"57833bf8cb3099a383e8e2af\"\r\n" +
" },\r\n" +
" \"Name\": \"3GBWS\",\r\n" +
" \"Version\": \"QV3.2\",\r\n" +
" \"Type\": \"FQGA\",\r\n" +
" \"SerialNO\": \"L1D73708884\",\r\n" +
" \"Location\": \"TEXAS\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"_id\": {\r\n" +
" \"$oid\": \"5784818bcb30b4918964b50f\"\r\n" +
" },\r\n" +
" \"Name\": \"3GBTW\",\r\n" +
" \"Version\": \"WN6.0\",\r\n" +
" \"Type\": \"FQGW\",\r\n" +
" \"SerialNO\": \"O1143734584\",\r\n" +
" \"Location\": \"OHIO\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"_id\": {\r\n" +
" \"$oid\": \"5784818bcb30b4918964b50f\"\r\n" +
" },\r\n" +
" \"Name\": \"TEXAS\",\r\n" +
" \"Version\": \"AS1.0\",\r\n" +
" \"Type\": \"FWQA\",\r\n" +
" \"SerialNO\": \"DH783708884\",\r\n" +
" \"Location\": \"NY\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"_id\": {\r\n" +
" \"$oid\": \"5784818bcb30b4918964b50f\"\r\n" +
" },\r\n" +
" \"Name\": \"3GLTS\",\r\n" +
" \"Version\": \"WE9.0\",\r\n" +
" \"Type\": \"FQGW\",\r\n" +
" \"SerialNO\": \"L0943708884\",\r\n" +
" \"Location\": \"TEXAS\"\r\n" +
" }\r\n" +
"]";
JSONArray array = new JSONArray(json);
int count = 0;
for(int i = 0; i < array.length(); i++) {
JSONObject element = array.getJSONObject(i);
String location = element.getString("Location");
if(location.equals("TEXAS")) {
count ++;
}
}
System.out.println("TEXAS=" + count);
}
}
I'd recommend you generate your POJO classes with an online tool (e.g. http://www.jsonschema2pojo.org/) and then you can simply manipulate them anyway you want. For example with a static method:
public static int countLocationOccurrences(List<YourClass> collections, String location) {
int count = 0;
for(YourClass item : collections) {
if (item.getLocation().equals(location)) {
count++;
}
}
return count;
}
First filter some values:
locations = j.map(elem => elem.Location) // j is the array
after this you well have an array with the locations, and you can do with it as you like.

Categories

Resources