i create two json string using GSON now i want to insert one json string to another but i don't understand how to I achieve this
json string 1
{"floors":[{"coveredArea":"200","floorId":"12","uploaded":false,"id":1},{"coveredArea":"200","floorId":"12","uploaded":false,"id":5}]}
json String 2
"surveys":[{"UId":"2","occupier": "rent","propertyId": "3","formId":1605250886166,"isSubmitted":false,"id":1}]}
but i want to this like this result
{
"surveys": [{
"propertyId": "3",
"UId": "161",
"occupier": "rent",
"formId": 1605250886166,
"isSubmitted": true,
"floor": [{
"floorId": "200",
"coveredArea": "200"
},
{
"floorId": "200",
"coveredArea": "200"
}
]
}]
}
Related
I have a Json body like the example below. I need to extract the value from a key that has another key with a specific value in an array. I am passing in a JsonNode with everything in the detail component of the message, I can easily extract from each level, however, I'm struggling with the array.
In this case, I need to extract the value of "value" (Police/Fire/Accident Report) from the object in the array which has a key/value pair of "name":"documentTitle". I understand this is a JSONArray, but I can't find a good example that shows me how to extract the values for an object in the array that contains a certain key/value pair, I don't think I can rely on getting the object in position [2] in the array as the same objects may not always be present in the additionalMetadata array.
Sample Json:
"sourceVersion": "1.0",
"eventId": "8d74b892-810a-47c3-882b-6e641fd509eb",
"clientRequestId": "b84f3a7b-03cc-4848-a1e8-3519106c6fcb",
"detail": {
"stack": "corona",
"visibilityIndicator": null,
"documentUid": "b84f3a7b-03cc-4848-a1e8-3519106c6fcb",
"additionalMetadata": [
{
"name": "lastModifiedDate",
"value": "2021-05-21T04:53:53Z"
},
{
"name": "documentName",
"value": "Police/Fire Report, 23850413, 2021-05-20 14:51:23"
},
{
"name": "documentTitle",
"value": "Police/Fire/Accident Report"
},
{
"name": "documentAuthor",
"value": "System Generated"
},
{
"name": "lastModifiedBy",
"value": "System Updated"
},
{
"name": "createdBy",
"value": "System Generated"
},
{
"name": "documentDescription",
"value": "Police/Fire Report received"
},
{
"name": "organizationCode",
"value": "Claims"
}
]
}
}```
Loop through the json array and extract the json object with name documentTitile. From that json object you can get the value
Well, either the JSON framework you're using supports this out of the box (check the documentation) or you could convert it manually to a map:
List<AdditionalMetadataEntry> additionalMetadata;
[...]
Map<String, String> additionalMetadataMap = additionalMetadata.stream().collect(Collectors.toMap(AdditionalMetadataEntry::getName, AdditionalMetadataEntry::getValue));
I was able to figure it out. I created a new node off the existing notificationBody JsonNode, then parsed through the metadata key/value pairs:
String docTitle = "";
JsonNode additionalMetadata = notificationBody.get("detail").get("additionalMetadata");
for (JsonNode node: additionalMetadata) {
String name = node.get("name").asText();
String value = node.get("value").asText();
if(name.equals("documentTitle")){
docTitle = value;
}
}
I have a Json object like this in my Postgres DB:
{
"emails": [
{
"email": {
"id": "e8dc927f-679d-496b-85fb-465edf35c676",
"value": "hello#gmail.com"
}
},
{
"email": {
"id": "1b78758a-abc4-46ef-9de9-c999a0c8c418",
"value": "hello1#gmail.com"
}
}
],
"lastName": {
"id": "718109fd-2d00-475a-829a-c8af9a7f0067",
"value": "lastName"
},
"firstName": {
"id": "6c46a5b3-6f89-4692-a214-4943de22018d",
"value": "firstName"
},
}
And so on big json with around 1000 elements, now I want to parse and get the first 500 elements from json and make another json. what I mean by element here is anything which has Id is a element. For example firstName , LastName, email, email are the elements not the emails. I tried Jackson api but couldn't find a way how to count the elements and make a json exactly like above and return. and when I do any modifications in the first 500 elements I should save the Json with edits. Any help is much appreciated. I even tried postgres array_agg(e) function but that is only accepting only array.
I am building an Android application which reads from themoviedb.org.
What I am trying to do is have the user enter a movie title and use that title to find its id.
When I run the query to search for movies, I get a response like:
{
"page": 1,
"results": [
{
"poster_path": "aaaaa.jpg",
"id": "11",
"description": "MovieDescription"
},
{
"poster_path": "bbbbb.jpg",
"id": "12",
"description": "MovieDescription2"
},
{
"poster_path": "ccccc.jpg",
"id": "13",
"description": "MovieDescription"
}
]
}
Using the Maven JSON library, I can fetch the results key as a string using json.get("results").
returning:
[
{
"poster_path": "aaaaa.jpg",
"id": "11",
"description": "MovieDescription"
},
{
"poster_path": "bbbbb.jpg",
"id": "12",
"description": "MovieDescription2"
},
{
"poster_path": "ccccc.jpg",
"id": "13",
"description": "MovieDescription"
}
]
But I want to convert the first of these results to another JSONObject so that I can get the movie's id from the first result.
I'm thinking that the way to do this is to convert the results value to a list of JSONObject and then use the json.get("id") method on the first object in the list. But I do not know how to do this conversion.
Any help would be appreciated.
You can use JSONObject.getJSONArray to get the result directly as a JSON Array:
JSONArray results = json.getJSONArray("results") // Get results as JSON Array
JSONObject first = results.getJSONObject(0) // Get first object as JSON Object
See: JSONObject#getJSONArray(String)
Given I have the following json structure:
{
"Role": {
"id": "5",
"name": "bb1",
"description": "desc1",
"PermissionDeck": [
{
"id": "7",
"Permission": [
{
"id": "398"
},
{
"id": "399"
},
{
"id": "400"
},
{
"id": "401"
},
{
"id": "402"
}
],
"Limit": [
{
"id": "4"
},
{
"id": "5"
}
]
}
]
}
}
If I want to cast this into a LinkedTreeMap result so that its content could be a retrieved by:
result.get("Role") returns Map
and
result.get("Role").get("PermissionDeck").size() == 5
and
result.get("Role").get("PermissionDeck").get(0).get("id") == 398
basically makes gson.fromjson recursively go into the structure, and fold any nested structure into LinkedTreeMap until it gets to the most inner layer, which then gets into LinkedTreeMap
Is this possible without writing custom recursive methods?
You can't. The closest you'll get is with JsonObject and using the appropriate getter for each nested member. In other words, your code needs to be explicit about what it expects in the JSON.
For example
JsonObject result = new Gson().fromJson(theJson), JsonObject.class);
System.out.println(result.getAsJsonObject("Role").getAsJsonArray("PermissionDeck").size());
will print 1 since you only have one element in the JSON array named PermissionDeck in the JSON object named Role in the root JSON object.
In my project when i send Rest Response to Advance Rest Client It only shows Fields which Have some values and Ignores(Does not show) fields which have NULL Values or Empty values.
Part Of Code:
Gson gson=new Gson();
// firstResponse is Object which contains the values
String jsonString = gson.toJson(firstResponse);
test.saveJson(jsonString); //OR System.out.println(jsonString);
return Response.ok(firstResponse).build(); // Response to Rest Client
Response sample To return Response.ok(firstResponse).build();
Advance rest client From web project :
{
"Name": "smith",
"Properties": {
"propertyList": [
{
"ID": "072",
"Number": "415151",
"Address": "Somewhere"
},
{
"ID": "151",
"Number": "a800cc79-99d1-42f1-aeb4-808087b12c9b",
"Address": "ninink"
},
{
"ID": "269",
},
],
},
}
Now when i save this as Json String in DB or When i want to Print this to console it also prints the fiels with null or empty values:
{
"Name": "smith",
"Properties": {
"propertyList": [
{
"ID": "072",
"Number": "415151",
"Address": "Somewhere"
},
{
"ID": "151",
"Number": "a800cc79-99d1-42f1-aeb4-808087b12c9b",
"Address": "ninink"
},
{
"ID": "269",
"Number": "",
"Address": ""
},
],
},
"resultList" :[]
}
How can i print or save this JSON string same as response in rest client i.e. i dont want to print null or empty value field i just want to ignore them.
in top og entity class , try with the annotation
#JsonInclude(Include.NON_EMPTY)
this annotation don't show any empty field in your json.
Not giving you a code but here are some pointers for you:
Read the manual: How to handle NULL values
You may need to use a custom exclusion strategy
Also read this Q&A: Gson: How to exclude specific fields from Serialization without annotations