This is my sample Json
{
"State": {
"version": "1",
"SName": "Test",
"shippingDetails": {
"Address1": "AP",
"ZipCode": "1236"
},
"Directions": {
"routes": [
{
"taxAmount": "0.0",
"Quantity": "5",
"bounds": {
"SerialVersion": [
{
"text": "1.7 km",
"value": "1729",
"time": "02633"
},
{
"text": "1.9 km",
"value": "1829",
"time": "02353"
},
{
"text": "17 km",
"value": "1059",
"time": "02133"
}
]
}
}
]
}
}
}
I want to update SName, ZipCode,taxAmount,Quantity, and text[1] values
are there any way to do this. I am taking JSON in a file and update tags are taking into HashMap
JSONObject jsonObject = new JSONObject("Your_JSON_String");
JSONObject jsonObjectForState = jsonObject.getJSONObject(“State”);
jsonObjectForState.put("Sname", "New_Value_Here");
put(...) will replace the current value with new value. Similarly, you can update the other values. Once you are done, you can convert it back using:
jsonObject.toString();
And write it back to the file.
Related
How to get a new list from the list of objects?
I need a new list of objects to POST request
this list of objects i get from response:
{
"success": true,
"body": {
"users": [
{
"type": "unknown",
"data": [
{
"id": "8",
"firstName": "Jackson",
"lastName": "Baker",
"group": "false"
},
{
"id": "11",
"firstName": "Charlotte",
"lastName": "Garcia",
"group": "false"
},
{
"id": "7",
"firstName": "Henry",
"lastName": "Thompson",
"group": "false"
},
{
"id": "24",
"firstName": "Elijah",
"lastName": "Miller",
"group": "false"
}
]
}
]
}
}
I need to form a new object from response:
{
"success": true,
"body": {
"users": [
{
"type": "unknown",
"data": [
{
"id": "8",
"group": "false"
},
{
"id": "11",
"group": "false"
},
{
"id": "7",
"group": "false"
},
{
"id": "24",
"group": "false"
}
]
}
]
}
}
This new list of object I need to post a request.
I have a JSON Schema, but i don't know how i can make this structure.
My problem is that I can't get the values from fields. I'm using the path "body.users.data.find {it.id} .id" but it finds the id array but I need a users list with values from two fields.
As the users node and data node are all list type, you need use two loop:
import groovy.json.JsonSlurper
// text is your json response text
def result = new JsonSlurper().parseText(text)
result.body.users.each{it.data.each{it.remove("firstName");it.remove("lastName")}}
// result is the what you wanted
println(result)
I need to extract a json in required format from input json. i'm using jayway json path library. How to achieve it ?
Input Json:
{
"ccid": [
{
"id": 13,
"src": {
"sname": "XA-SXXD",
"lname": "John",
"identifier": 2,
"StatusCode": "C"
}
},
{
"id": 14,
"src": {
"sname": "XB-SXXD",
"lname": "Cena",
"identifier": 3,
"StatusCode": "C",
}
}
]
}
Required Format:
[ {
"id": "13",
"sources": {
"sname": "XA-SXXD",
"lname": "John",
"identifier": 2
}
},
{
"id": "14",
"sources": {
"sname": "XB-SXXD",
"lname": "Cena",
"identifier": 3
}
}]
Query that i use:
$.ccid[*].src[?(#.identifier!=null)].['identifier','sname']
Output that i get:
[
{
"identifier" : 2,
"sname" : "XA-SXXD"
},
{
"identifier" : 3,
"sname" : "XB-SXXD"
}
]
Kindly help me to modify my query to get the required format. The string "sources" in the required format can be hardcoded.
I think I managed to solve this :)
$.ccid[*].[?(#.src.identifier!=null)].['id', 'src']
Give it a try.
Input tested on:
{
"ccid": [
{
"id": 13,
"src": {
"sname": "XA-SXXD",
"lname": "John",
"StatusCode": "C"
}
},
{
"id": 14,
"src": {
"sname": "XB-SXXD",
"lname": "Cena",
"identifier": null,
"StatusCode": "C",
}
}
]
}
Output received:
[
{
"id" : 13,
"src" : {
"sname" : "XA-SXXD",
"lname" : "John",
"StatusCode" : "C"
}
}
]
Only problem is saw is if identifier tag is not available it's essentially treated as not-null. Hence we are getting 13 as an output. But if value is explicitly null then it's fine. So the needs to be enhanced a bit more.
Hope this helps.
Very new to JSON. Using json-lib for this and been pulling my hair out trying to get "definitions" from this JSON response by Oxford Dictionaries API. Tried all sorts of things. But can never get anything more specific than everything from "results". Would like to figure out why this is so difficult otherwise will just resort to regex.
JSONObject obj = new JSONObject(stringBuilder.toString());
JSONArray arr = obj.getJSONArray("results");
String test = arr.getJSONObject(1).toString(); // Empty wtf?
String definition = obj.getString("definitions"); // empty also...
Managed to also access "id", "language" and "lexicalEntries" separately, but anything beyond that doesn't seem to want to cooperate.... Is this a normal JSON response? It's quite awkward no? Thanks...
JSON:
{
"metadata": {
"provider": "Oxford University Press"
},
"results": [
{
"id": "ace",
"language": "en",
"lexicalEntries": [
{
"entries": [
{
"homographNumber": "000",
"senses": [
{
"definitions": [
"a playing card with a single spot on it, ranked as the highest card in its suit in most card games"
],
"id": "m_en_gbus0005680.006"
},
{
"definitions": [
"a person who excels at a particular sport or other activity"
],
"id": "m_en_gbus0005680.010",
"subsenses": [
{
"definitions": [
"a pilot who has shot down many enemy aircraft"
],
"id": "m_en_gbus0005680.011"
}
]
},
{
"definitions": [
"(in tennis and similar games) a service that an opponent is unable to return and thus wins a point"
],
"id": "m_en_gbus0005680.013",
"subsenses": [
{
"definitions": [
"a hole in one"
],
"id": "m_en_gbus0005680.014"
}
]
}
]
}
],
"language": "en",
"lexicalCategory": "Noun",
"text": "ace"
},
{
"entries": [
{
"homographNumber": "001",
"senses": [
{
"definitions": [
"very good"
],
"id": "m_en_gbus0005680.016"
}
]
}
],
"language": "en",
"lexicalCategory": "Adjective",
"text": "ace"
},
{
"entries": [
{
"homographNumber": "002",
"senses": [
{
"definitions": [
"(in tennis and similar games) serve an ace against (an opponent)"
],
"id": "m_en_gbus0005680.020",
"subsenses": [
{
"definitions": [
"score an ace on (a hole) or with (a shot)"
],
"id": "m_en_gbus0005680.026"
}
]
},
{
"definitions": [
"achieve high marks in (a test or exam)"
],
"id": "m_en_gbus0005680.028",
"subsenses": [
{
"definitions": [
"outdo someone in a competitive situation"
],
"id": "m_en_gbus0005680.029"
}
]
}
]
}
],
"language": "en",
"lexicalCategory": "Verb",
"text": "ace"
}
],
"type": "headword",
"word": "ace"
}
]
}
String jsonData = sb.toString();
JSONObject obj = new JSONObject(jsonData);
JSONArray resultsArr = obj.getJSONArray("results");
String test = resultsArr.getJSONObject(0).toString();
JSONArray lexicalEntriesArr = resultsArr.getJSONObject(0).getJSONArray("lexicalEntries");
JSONArray entriesArr = lexicalEntriesArr.getJSONObject(0).getJSONArray("entries");
JSONArray sensesArr = entriesArr.getJSONObject(0).getJSONArray("senses");
JSONArray definitionsArr = sensesArr.getJSONObject(0).getJSONArray("definitions");
String definition = definitionsArr.toString();
Reference: http://jsonviewer.stack.hu/
Watch carefully the way we traverse the JSON object and when we should use getJSONObject & getJSONArray methods to retrieve the particular type of data we want from JSON Object.
Update: You might want to iterate the JSON array as per your requirement. I have just explained the case as in how to reach till definitions.
I have a json something like this below .I want to read it and add 2 more attribute ti it like country and state
[
{
"id": "123",
"testname": "test123",
"name": "John Doe",
"active": true,
"type": "test6"
}
{
"id": "456",
"testname": "test564",
"name": "Ship Therasus",
"active": true,
"type": "test7"
}
]
Resulting json
[
{
"id": "123",
"testname": "test123",
"name": "John Doe",
"active": true,
"type": "test6",
"country":"USA",
"state":"KA"
}
{
"id": "456",
"testname": "test564",
"name": "Ship Therasus",
"active": true,
"type": "test7",
"country":"UK",
"state":"MA"
}
]
I am doing something like this I tried JSONObject but no output.
JSONArray xmlJSONObj2 = new JSONArray(output);
System.out.println("Output from Server .... \n"+xmlJSONObj2.get(0));
you can use for loop to iterate over JSONArray, Once you have jsonObject use put for extra attribute like this
for(int i=0;i<jsonArray.length();i++){
JSONObject json = jsonArray.getJSONObject(i);
// do this for all remaining field
if(json.has("id")){
String id = json.get("id").toString();
}
// finally put extra attributes to that jsonobject
json.put("country", "USA");
json.put("state", "KA")
}
missed commas after "type":"test6"
{
"id": "123",
"testname": "test123",
"name": "John Doe",
"active": true,
"type": "test6" <------
"country":"USA",
"state":"KA"
}
I got json[] from UI. I have to convert it into Json object how can I convert it with gson. An object which is received is like that:
[{"name":"name","value":"value"},{"name":"first","value":"100"},{"name":"hor","value":"95"},{"name":"conf","value":"95"}],[{"name":"vaRType","value":"INCRE"},{"name":"per","value":"100"},{"name":"hor","value":"95"},{"name":"conf","value":"95"}]
I used JsonObject obj = new JsoParser().parse(jsonStrnig).getAsJsonObject();
but it throws an exception.
Because it is not valid json object.
Your JSON should be the following as currently it's invalid...
[
[
{
"name": "name",
"value": "value"
},
{
"name": "first",
"value": "100"
},
{
"name": "hor",
"value": "95"
},
{
"name": "conf",
"value": "95"
}
],
[
{
"name": "vaRType",
"value": "INCRE"
},
{
"name": "per",
"value": "100"
},
{
"name": "hor",
"value": "95"
},
{
"name": "conf",
"value": "95"
}
]
]