How to extract specific data from json with multiple nodes? - java

Using java with selenium
I have captured full json in my code
IN my json there are two nodes "Service1" and "Service2"
My requirement is to fetch the details of "Services1" node -->data node
But i am not able to extract only Service1 with data node details(i do not want "test" node details.
Question: Can you please help me to extract data from Service1-with data node only using java code?I am using selenium with java.I need this for one of the script.
Actual response:
{
"Service1": [
{
"data": {
"status": false,
"type": "A",
"order": 1,
"Version": "v6"
},
"test": {
"ModuleID": "123456",
"BoxID": "a777",
"Url": "https://google.com"
}
},
{
"data": {
"status": true,
"type": "B",
"order": 1,
"Version": "v2"
},
"test": {
"ModuleID": "123456",
"BoxID": "a777",
"Url": "https://google.com"
}
}
],
"Service2": {
"data1": {
"status": false,
"type": "c",
"order": 1,
"Version": "v6"
},
"dashboard": {
"ModuleID": "123456",
"BoxID": "a777",
"Url": "https://google.com"
}
}
}
Expected data :
[
{
"status": false,
"type": "A",
"order": 1,
"Version": "v6"
},
{
"status": true,
"type": "B",
"order": 1,
"Version": "v2"
}
]

Fetching the required data is based on the api response. But in the result of the fetched data you can convert it as you want in java as:
Main theme:
JSONObject response = new JSONObject("json data");//replace json data with your given json data
JSONArray service1 = response.getJSONArray("Service1");
JSONObject firstObjectInService1 = service1.getJSONObject(0);
JSONObject secondObjectInService1 = service1.getJSONObject(1);
JSONObject dataInFirstObject = firstObjectInService1.getJSONObject("data");
JSONObject dataInSecondObject = secondObjectInService1.getJSONObject("data");
Full code snippet:
import org.json.*;
public class JsonParsing {
public static void main(String[] args) {
String jsonData = "{\n" +
"\"Service1\": [\n" +
" {\n" +
" \"data\": {\n" +
" \"status\": false,\n" +
" \"type\": \"A\",\n" +
" \"order\": 1,\n" +
" \"Version\": \"v6\"\n" +
" },\n" +
" \"test\": {\n" +
" \"ModuleID\": \"123456\",\n" +
" \"BoxID\": \"a777\",\n" +
" \"Url\": \"https://google.com\"\n" +
"\n" +
" }\n" +
"\n" +
" },\n" +
" {\n" +
" \"data\": {\n" +
" \"status\": true,\n" +
" \"type\": \"B\",\n" +
" \"order\": 1,\n" +
" \"Version\": \"v2\"\n" +
" },\n" +
" \"test\": {\n" +
" \"ModuleID\": \"123456\",\n" +
" \"BoxID\": \"a777\",\n" +
" \"Url\": \"https://google.com\"\n" +
"\n" +
"\n" +
" }\n" +
" }\n" +
"\n" +
"],\n" +
"\"Service2\": {\n" +
" \"data1\": {\n" +
" \"status\": false,\n" +
" \"type\": \"c\",\n" +
" \"order\": 1,\n" +
" \"Version\": \"v6\"\n" +
" },\n" +
" \"dashboard\": {\n" +
" \"ModuleID\": \"123456\",\n" +
" \"BoxID\": \"a777\",\n" +
" \"Url\": \"https://google.com\"\n" +
"\n" +
" }\n" +
"}\n" +
"}";
System.out.println(jsonData);
try {
JSONObject response = new JSONObject(jsonData);//replace json data with your given json data
JSONArray service1 = response.getJSONArray("Service1");
JSONObject firstObjectInService1 = service1.getJSONObject(0);
JSONObject secondObjectInService1 = service1.getJSONObject(1);
JSONObject dataInFirstObject = firstObjectInService1.getJSONObject("data");
JSONObject dataInSecondObject = secondObjectInService1.getJSONObject("data");
System.out.println(dataInFirstObject);
System.out.println(dataInSecondObject);
} catch (JSONException je) {
//do what you want
}
}
}
so finally in dataInFirstObject we have:
{
"status": false,
"type": "A",
"order": 1,
"Version": "v6"
}
and dataInSecondObject we have:
{
"status": true,
"type": "B",
"order": 1,
"Version": "v2"
}

You can use JSON simple library ,it is library used to read/write/parse data in JSON file.In this example i read data from JSON file not a JSON content directly.
private void findData() {
ArrayList<String> dataArrayList=new ArrayList<>();
try {
JSONParser parser = new JSONParser();
InputStream in = getClass().getResourceAsStream("/json/Services.json");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
Object obj = parser.parse(reader);
JSONObject jsonObject = (JSONObject) obj;
JSONArray msg = (JSONArray) jsonObject.get("Service1");
Iterator<JSONObject> iterator = msg.iterator();
while (iterator.hasNext()) {
JSONObject jsonObjectData = iterator.next();
JSONObject dataObject = (JSONObject) jsonObjectData.get("data");
System.out.println(dataObject);
//Extract values
dataObject.values().stream().forEach(value->{
System.out.println(value);
dataArrayList.add(value.toString());
});
}
} catch (IOException | org.json.simple.parser.ParseException ex) {
Logger.getLogger(CorporationRegistrationFormController.class.getName()).log(Level.SEVERE, null, ex);
}
}
If you need read JSON content from your code just replace the reader with your content like this : Object obj = parser.parse("JSON content");

Related

How to rewrite full updated json instead of only the nested part

I have a json file called test.json that looks like:
{
"Added": {
"type": "K",
"newmem": {
"IDNew": {
"id": "777709",
"type": "LOP"
},
"birthDate": "2000-12-09"
},
"code": "",
"newest": {
"curlNew": "",
"addedForNew": ""
}
}
}
I am able to update the id in the json and write back to the file using the following code:
FileReader reader = new FileReader(filePath); //where file path = D://.../test.json
String keyToUpdate = "Added.newmem.IDNew.id"
String[] keyArr = jsonKey.split("\\." );
String keyToUpdate = keyArr [keyArr.length-1]
keyArr.removeElement(keyArr, keyToUpdate)
for (String key: keyArr){
jsonObject = (JSONObject) jsonObject.get(key);
}
jsonObject.put("id",12345);
FileOutputStream outputStream = new FileOutputStream(filePath);
byte[] strToBytes = jsonObject.toString().getBytes();
outputStream.write(strToBytes);
However, instead of re-writing the entire json with the updated value, it's only writing:
{
"id": "777709",
"type": "LOP"
}
What can I do to re-write the entire json instead of only the nested part of it?
Your jsonObject before update only contains the "IDNew" node.
The best way to do this is using xPath from Jayway
private static final Configuration configuration = Configuration.builder()
.jsonProvider(new JacksonJsonNodeJsonProvider())
.mappingProvider(new JacksonMappingProvider())
.build();
String originalJson = "{\r\n"
+ " \"Added\": {\r\n"
+ " \"type\": \"K\",\r\n"
+ " \"newmem\": {\r\n"
+ " \"IDNew\": {\r\n"
+ " \"id\": \"777709\",\r\n"
+ " \"type\": \"LOP\"\r\n"
+ " },\r\n"
+ " \"birthDate\": \"2000-12-09\"\r\n"
+ " },\r\n"
+ " \"code\": \"\",\r\n"
+ " \"newest\": {\r\n"
+ " \"curlNew\": \"\",\r\n"
+ " \"addedForNew\": \"\"\r\n"
+ " }\r\n"
+ " }\r\n"
+ "}";
JsonNode updatedJson = JsonPath.using(configuration).parse(originalJson).set("$.Added.IDNew.id", 12345).json();
System.out.println(updatedJson.toString());
}

Getting the values of a specific key from JsonNode

I have a JsonNode result represented as:
[
{
"item": {
"type": "uri",
"value": "http://www.wikidata.org/entity/Q42442324"
},
"prop": {
"type": "uri",
"value": "http://www.wikidata.org/prop/direct/P21"
},
"itemLabel": {
"xml:lang": "en",
"type": "literal",
"value": "Kiisu Miisu"
}
},
{
"item": {
"type": "uri",
"value": "http://www.wikidata.org/entity/Q43260736"
},
"prop": {
"type": "uri",
"value": "http://www.wikidata.org/prop/direct/P21"
},
"itemLabel": {
"xml:lang": "en",
"type": "literal",
"value": "Paddles"
}
}
]
I am trying to retieve the values of the key "value" into an array list using the below code but am getting the error
Cannot invoke "com.fasterxml.jackson.databind.JsonNode.findValue(String)" because the return value of "com.fasterxml.jackson.databind.JsonNode.get(int)" is null
for (int i = 0; i < resultSize; i++) {
JsonNode jsonObject = results.get(i);
if (indexRow < jsonObject.size()) {
jsonRows = Collections.singletonList(jsonObject.get(indexRow++).findValue("value").asText());
}
}
The value of variable jsonObject in the first iteration from the debugger is
{
"item": {
"type": "uri",
"value": "http://www.wikidata.org/entity/Q42442324"
},
"prop": {
"type": "uri",
"value": "http://www.wikidata.org/prop/direct/P21"
},
"itemLabel": {
"xml:lang": "en",
"type": "literal",
"value": "Kiisu Miisu"
}
}
Expected output is
[
"http://www.wikidata.org/entity/Q42442324",
"http://www.wikidata.org/entity/Q42442324",
"Kiisu Miisu",
"http://www.wikidata.org/entity/Q43260736",
"http://www.wikidata.org/prop/direct/P21",
"Paddles"
]
You can use elements() method and check if value key exist then add the value to list.
Smaple code
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(data);
List<String> values = new ArrayList<>();
jsonNode.forEach(jsonObject -> jsonObject.elements().forEachRemaining(valueNode -> {
if(valueNode.has("value"))
values.add(valueNode.get("value").asText());
}));
System.out.println(values);
Output:
[http://www.wikidata.org/entity/Q42442324, http://www.wikidata.org/prop/direct/P21, Kiisu Miisu, http://www.wikidata.org/entity/Q43260736, http://www.wikidata.org/prop/direct/P21, Paddles]
Here is the solution by "Josson & Jossons". I list 2 more examples with condition filtering.
https://github.com/octomix/josson
implementation 'com.octomix.josson:josson:1.3.22'
---------------------------------------------
Josson josson = Josson.fromJsonString(
"[" +
" {" +
" \"item\": {" +
" \"type\": \"uri\", \"value\": \"http://www.wikidata.org/entity/Q42442324\"" +
" }," +
" \"prop\": {" +
" \"type\": \"uri\", \"value\": \"http://www.wikidata.org/prop/direct/P21\"" +
" }," +
" \"itemLabel\": {" +
" \"xml:lang\": \"en\", \"type\": \"literal\", \"value\": \"Kiisu Miisu\"" +
" }" +
" }," +
" {" +
" \"item\": {" +
" \"type\": \"uri\", \"value\": \"http://www.wikidata.org/entity/Q43260736\"" +
" }," +
" \"prop\": {" +
" \"type\": \"uri\", \"value\": \"http://www.wikidata.org/prop/direct/P21\"" +
" }," +
" \"itemLabel\": {" +
" \"xml:lang\": \"en\", \"type\": \"literal\", \"value\": \"Paddles\"" +
" }" +
" }" +
"]");
JsonNode node = josson.getNode("*.value");
System.out.println("1.\n" + node.toPrettyString());
node = josson.getNode("~'^item.*'.value");
System.out.println("2.\n" + node.toPrettyString());
node = josson.getNode("*[value.type='uri']*.value");
System.out.println("3.\n" + node.toPrettyString());
Output:
1.
[ "http://www.wikidata.org/entity/Q42442324", "http://www.wikidata.org/prop/direct/P21", "Kiisu Miisu", "http://www.wikidata.org/entity/Q43260736", "http://www.wikidata.org/prop/direct/P21", "Paddles" ]
2.
[ "http://www.wikidata.org/entity/Q42442324", "Kiisu Miisu", "http://www.wikidata.org/entity/Q43260736", "Paddles" ]
3.
[ "http://www.wikidata.org/entity/Q42442324", "http://www.wikidata.org/prop/direct/P21", "http://www.wikidata.org/entity/Q43260736", "http://www.wikidata.org/prop/direct/P21" ]

Add json inner object

I have the following code
JSONArray jobj = new select_data().get_patient_summary(p_id);
JSONObject jInnerObject = new JSONObject();
jInnerObject.put("weight", weight.get(0));
jobj.put(jInnerObject);
The outcome is:
{
"data": [
{
"pre_transfusional_haemoglobin_level": "",
"rhesus_blood_group_system": "",
"patient_id": "1",
"surname": "Demetriou",
"DOB": "2004-02-04",
"health_insurance_information": "ac",
"name": "Andreas",
"blood_group": "",
"transfusion_frequency": ""
},
{
"weight": "90"
}
],
"success": 1
}
Can I insert "weight" in the previous string, like:
{
"data": [
{
"pre_transfusional_haemoglobin_level": "",
"rhesus_blood_group_system": "",
"patient_id": "1",
"surname": "Demetriou",
"DOB": "2004-02-04",
"health_insurance_information": "ac",
"name": "Andreas",
"blood_group": "",
"transfusion_frequency": "",
"weight": "90"
},
],
"success": 1
}
EDITED for complete executable code:
import org.json.JSONObject;
public class test {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
String input = "{\r\n" +
" \"data\": [\r\n" +
" {\r\n" +
" \"pre_transfusional_haemoglobin_level\": \"\",\r\n" +
" \"rhesus_blood_group_system\": \"\",\r\n" +
" \"patient_id\": \"1\",\r\n" +
" \"surname\": \"Demetriou\",\r\n" +
" \"DOB\": \"2004-02-04\",\r\n" +
" \"health_insurance_information\": \"ac\",\r\n" +
" \"name\": \"Andreas\",\r\n" +
" \"blood_group\": \"\",\r\n" +
" \"transfusion_frequency\": \"\"\r\n" +
" },\r\n" +
" ],\r\n" +
" \"success\": 1\r\n" +
"}";
JSONObject jobj = new JSONObject(input);
jobj.getJSONArray("data").getJSONObject(0).put("weight", 90);
System.out.println(jobj.toString(1));
}
}

Build new json string using a json template file and a json data file

I have a json template file as below
{
"value": "abc",
"Treatments": [{
"value": "def",
"Stages": [{
"value": "ghi"
}]
}]
}
And a json data
{ "abc": {
"labelabc": "Assembly name abc",
"typeabc": "STRING_TYPE abc",
"formatabc": "N abc"
},
"def": {
"labeldef": "Assembly name def",
"typedef": "STRING_TYPE def",
"formatdef": "N def"
},
"ghi": {
"labelghi": "Assembly name ghi",
"typeghi": "STRING_TYPE ghi",
"formatghi": "N ghi"
}
}
I'm looking for a solution to parse the template into
{
"labelabc": "Assembly name abc",
"typeabc": "STRING_TYPE abc",
"formatabc": "N abc",
"Treatments": [
{
"labeldef": "Assembly name def",
"typedef": "STRING_TYPE def",
"formatdef": "N def",
"Stages": [
{
"labelghi": "Assembly name ghi",
"typeghi": "STRING_TYPE ghi",
"formatghi": "N ghi"
}
]
}
]
}
Meant:
Replace "value" entry with the jsonobject value in json data file
I already used the code below to achieve the result with 2 levels template. But failed with above 3 levels template
public static JsonElement generateTemplate(JsonElement templateJson, JsonElement dataTemplate) {
if (templateJson.isJsonArray()) {
for (JsonElement jsonElement1 : templateJson.getAsJsonArray()) {
generateTemplate(jsonElement1, dataTemplate);
}
} else if (templateJson.isJsonObject()) {
for (Iterator<Map.Entry<String, JsonElement>> iterator = templateJson.getAsJsonObject().entrySet()
.iterator(); iterator.hasNext();) {
Map.Entry<String, JsonElement> entry = iterator.next();
if (entry.getKey().equals("value")) {
templateJson = dataTemplate.getAsJsonObject().get(entry.getValue().getAsString()).getAsJsonObject();
} else {
if (entry.getValue().isJsonObject()) {
generateTemplate(entry.getValue(), dataTemplate);
} else
templateJson.getAsJsonObject().add(entry.getKey(), p.parse(
dataTemplate.getAsJsonObject().get(entry.getValue().getAsString()).getAsJsonObject()
));
}
generateTemplate(entry.getValue(), dataTemplate);
}
}
return templateJson;
}
Very appreciated your advices
You could use recursion. This is main idea, you can adjust actual replacements according to your needs later in modifyObject method which for "value": "abc" would expect object held in data under
"abc": {
"labelabc": "Assembly name abc",
"typeabc": "STRING_TYPE abc",
"formatabc": "N abc"
}
private static void modifyObject(JsonObject obj, JsonObject replacement) {
obj.remove("value");
for (String key : replacement.keySet()) {
obj.addProperty(key, replacement.get(key).getAsString());
}
}
public static JsonElement traverse(JsonElement element, JsonObject allReplacements) {
if (element.isJsonObject()) {
JsonObject asJsonObject = element.getAsJsonObject();
///keys can change after we modify object,
//but we want to iterate only over original keys
Set<String> originalKeys = new HashSet<>(asJsonObject.keySet());
for (String key : originalKeys) {
if (key.equals("value")) {
String value = asJsonObject.get(key).getAsString();
modifyObject(asJsonObject, allReplacements.getAsJsonObject(value));
} else {
traverse(asJsonObject.get(key), allReplacements);
}
}
} else if (element.isJsonArray()) {
for (JsonElement innerElement : element.getAsJsonArray()) {
traverse(innerElement, allReplacements);
}
}
return element;
}
Usage:
public static void main(String[] args) throws Exception {
String jsonTemplate =
"{\n" +
" \"value\": \"abc\",\n" +
" \"foo\": \"bar\",\n" +
" \"Treatments\": [ {\n" +
" \"value\": \"def\",\n" +
" \"Stages\": [ [ {\n" +
" \"value\": \"ghi\"\n" +
" } ] ]\n" +
" } ]\n" +
"}";
String jsonData = "{ \"abc\": {\n" +
" \"label\": \"Assembly name abc\",\n" +
" \"type\": \"STRING_TYPE abc\",\n" +
" \"format\": \"N\"\n" +
" },\n" +
" \"def\": {\n" +
" \"label\": \"Assembly name def\",\n" +
" \"type\": \"STRING_TYPE\",\n" +
" \"format\": \"N\"\n" +
" },\n" +
" \"ghi\": {\n" +
" \"label\": \"Assembly name ghi\",\n" +
" \"type\": \"STRING_TYPE\",\n" +
" \"format\": \"N\"\n" +
" }\n" +
"}";
JsonParser jsonParser = new JsonParser();
JsonElement template = jsonParser.parse(jsonTemplate);
JsonObject data = (JsonObject) jsonParser.parse(jsonData);
JsonElement obj = template.deepCopy();//in case we don't want to modify original template
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(traverse(obj, data)));
}
Output:
{
"foo": "bar",
"Treatments": [
{
"Stages": [
[
{
"label": "Assembly name ghi",
"type": "STRING_TYPE",
"format": "N"
}
]
],
"label": "Assembly name def",
"type": "STRING_TYPE",
"format": "N"
}
],
"label": "Assembly name abc",
"type": "STRING_TYPE abc",
"format": "N"
}

How to read multiple json objects using java?

I have a JSON response and I want to store each element in a string. as I am new to JSON, its difficult to find the solution. please suggest me a solution.
the below is my json response.
{
"responseFlag": 1,
"responseMsg": "Successfully retrieved data",
"responseObj": [{
"assets": {
"asset_since": "",
"asset_type": "",
"comments": "",
"estimated_value": "",
"material_status": "SINGLE",
"ownership_of_assets": "",
"pep": "",
"source_of_income": ""
}
},
{
"assets": {
"asset_since": "",
"asset_type": "",
"comments": "",
"estimated_value": "",
"material_status": "SINGLE",
"ownership_of_assets": "",
"pep": "",
"source_of_income": ""
}
}
]
}
I want to store each object elements in an array.
the code I have tried is below.
package mytry;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class Mytry {
public static void main(String[] args) {
// TODO code application logic here
String response="{\n" +
" \"responseFlag\": 1,\n" +
" \"responseMsg\": \"Successfully retrieved data\",\n" +
" \"responseObj\": [\n" +
" {\n" +
" \"assets\": {\n" +
" \"asset_since\": \"\",\n" +
" \"asset_type\": \"\",\n" +
" \"comments\": \"\",\n" +
" \"estimated_value\": \"\",\n" +
" \"material_status\": \"SINGLE\",\n" +
" \"ownership_of_assets\": \"\",\n" +
" \"pep\": \"\",\n" +
" \"source_of_income\": \"\"\n" +
" }},\n" +
" {\n" +
" \"assets\": {\n" +
" \"asset_since\": \"\",\n" +
" \"asset_type\": \"\",\n" +
" \"comments\": \"\",\n" +
" \"estimated_value\": \"\",\n" +
" \"material_status\": \"SINGLE\",\n" +
" \"ownership_of_assets\": \"\",\n" +
" \"pep\": \"\",\n" +
" \"source_of_income\": \"\"\n" +
" }}]}";
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(response);
JSONObject jsonObject = (JSONObject) obj;
//System.out.println(jsonObject.toString());
System.out.println("json size=="+jsonObject.size());
System.out.println("hghgfh"+jsonObject.keySet());
Long sflag = (Long) jsonObject.get("responseFlag");
String msg=(String) jsonObject.get("responseMsg");
String resobj=(String) jsonObject.get("responseObj").toString();
//jsonObject.
System.out.println("sflag=="+sflag);
System.out.println("msg=="+msg);
System.out.println("msg=="+resobj);
// JSONArray msg = (JSONArray) jsonObject.get("responseFlag");
// Iterator<String> iterator = msg.iterator();
// while (iterator.hasNext()) {
// System.out.println(iterator.next());
// }
// for(Iterator iterator = jsonObject.keySet().iterator(); iterator.hasNext();) {
// String key = (String) iterator.next();
// System.out.println(jsonObject.get(key));
//}
// String asset = (String) jsonObject.get("assets");
// System.out.println("session token"+asset);
//sflag = (Long) jsonObject.get("responseFlag");
//System.out.println("session sflag"+sflag);
} catch (ParseException ex) {
System.out.println(ex);
}
}
}
the response object is
[{
"assets": {
"comments": "",
"asset_since": "",
"material_status": "SINGLE",
"source_of_income": "",
"ownership_of_assets": "",
"asset_type": "",
"pep": "",
"estimated_value": ""
}
}, {
"assets": {
"comments": "",
"asset_since": "",
"material_status": "SINGLE",
"source_of_income": "",
"ownership_of_assets": "",
"asset_type": "",
"pep": "",
"estimated_value": ""
}
}]
I need each asset values to be stored in an array.
Here is a pseudo code. You can fill the missing parts in this code.
String json = "{"responseFlag":1,"responseMsg":"Successfully retrieved data","responseObj":[{"assets":{"asset_since":"","asset_type":"","comments":"","estimated_value":"","material_status":"SINGLE","ownership_of_assets":"","pep":"","source_of_income":""}},{"assets":{"asset_since":"","asset_type":"","comments":"","estimated_value":"","material_status":"SINGLE","ownership_of_assets":"","pep":"","source_of_income":""}}]}";
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("responseObj");
for(int i=0; i<jsonArray.length(); i++)
{
JSONObject arrayJsonObject = jsonArray.getJSONObject(i);
//insert into your list or array
}
If you are using using json-simple-1.1.1 jar. here is the code below:
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(response);
JSONObject jsonObject = (JSONObject) obj;
//System.out.println(jsonObject.toString());
System.out.println("json size==" + jsonObject.size());
System.out.println("hghgfh" + jsonObject.keySet());
JSONArray jsonArray = (JSONArray)jsonObject.get("responseObj");
for(int i=0; i<jsonArray.size(); i++)
{
JSONObject arrayJsonObject = (JSONObject) jsonArray.get(i);
JSONObject assets = (JSONObject) arrayJsonObject.get("assets");
// read the assets to store
}
}catch (Exception e){
}

Categories