Narasimha
Narasimha

Reputation: 41

How to combine two or more JSON objects into one JSONObject

I've two JSON Objects as below

firstJSON: {"200":"success"} 
secondJSON: {"401":"not found"}

I need to combine them as {"codes":{"200":"success"},{"401":"not found"}} using java.

I tried in 3 ways but couldn't achieve the desired output. Could you please help with code snippet?

code which I've tried as below

JSONObject firstJSON = new JSONObject();
firstJSON.put("200", "success");
String firstJSONStr = firstJSON.toString();
System.out.println("firstJSONStr--> " + firstJSONStr);

JSONObject secondJSON = new JSONObject();
secondJSON.put("401", "not found");
String secondJSONStr = secondJSON.toString();
System.out.println("secondJSONStr--> "+secondJSONStr);

String finalJSONStr = firstJSONStr + "," + secondJSONStr;
JSONObject finalJSON1 = new JSONObject();
finalJSON1.put("codes", new JSONObject(finalJSONStr));
System.out.println("finalJSON1--> " + finalJSON1.toString());

JSONObject finalJSON2 = new JSONObject();
finalJSON2.put("codes", finalJSONStr);
System.out.println("finalJSON2--> " + finalJSON2.toString());

JSONObject finalJSON3 = new JSONObject();
ArrayList<JSONObject> jsonArray = new ArrayList<JSONObject>();
jsonArray.add(firstJSON);
jsonArray.add(secondJSON);
finalJSON3.put("codes", jsonArray);
System.out.println("finalJSON3--> " + finalJSON3.toString());

Output:

firstJSONStr--> {"200":"success"}
secondJSONStr--> {"401":"not found"}
finalJSON1--> {"codes":{"200":"success"}}
finalJSON2--> {"codes":"{\"200\":\"success\"},{\"401\":\"not found\"}"}
finalJSON3--> {"codes":[{"200":"success"},{"401":"not found"}]}

Upvotes: 3

Views: 288

Answers (2)

Narasimha
Narasimha

Reputation: 41

Got the logic finally.

            JSONObject successJSON = new JSONObject();
            successJSON.put("description", "success");

            JSONObject scJSON = new JSONObject();
            scJSON.put("200", successJSON);



            JSONObject failJSON = new JSONObject();
            failJSON.put("description","failure");


            scJSON.put("401", failJSON);


            JSONObject finalJSON = new JSONObject();
            finalJSON.put("codes", scJSON);

            System.out.println("finalJSON --> "+finalJSON.toString());

Upvotes: 0

Karol Dowbecki
Karol Dowbecki

Reputation: 44980

Your expected JSON {"codes":{"200":"success"},{"401":"not found"}} is invalid. You can verify it with https://jsonlint.com/ which will produce an error:

Error: Parse error on line 4:
...00": "success"   },  {       "401": "not foun
---------------------^
Expecting 'STRING', got '{'

You most likely want an array to group the first and second object which results in below JSON (do notice the square brackets [ and ]):

{"codes":[{"200":"success"},{"401":"not found"}]}

This can be achieved with:

JSONObject first = new JSONObject();
first.put("200", "success");

JSONObject second = new JSONObject();
second.put("401", "not found");

JSONArray codes = new JSONArray();
codes.put(first);
codes.put(second);

JSONObject root = new JSONObject();
root.put("codes", codes);

System.out.println(root);

Upvotes: 1

Related Questions