How to combine two or more JSON objects into one JSONObject












3















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"}]}









share|improve this question

























  • this might be helpful stackoverflow.com/questions/20346790/…

    – Supun Dharmarathne
    Jan 2 at 10:35











  • Note that your desired output is not valid JSON

    – Hulk
    Jan 2 at 10:35






  • 1





    I think the reason you are finding it difficult to get the json you require is because it simply isn't valid json: {"codes":{"200":"success"},{"401":"not found"}}. Here you have codes object {"200":"success"} and then an anonymous object {"401":"not found"}

    – karen
    Jan 2 at 10:35











  • hi folks, desired json which I'm trying to generate will be valid one

    – Narasimha
    Jan 2 at 11:09











  • @Narasimha {"codes":{"200":"success"},{"401":"not found"}} Is it valid JSON ?

    – varatharajan
    Jan 2 at 11:25
















3















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"}]}









share|improve this question

























  • this might be helpful stackoverflow.com/questions/20346790/…

    – Supun Dharmarathne
    Jan 2 at 10:35











  • Note that your desired output is not valid JSON

    – Hulk
    Jan 2 at 10:35






  • 1





    I think the reason you are finding it difficult to get the json you require is because it simply isn't valid json: {"codes":{"200":"success"},{"401":"not found"}}. Here you have codes object {"200":"success"} and then an anonymous object {"401":"not found"}

    – karen
    Jan 2 at 10:35











  • hi folks, desired json which I'm trying to generate will be valid one

    – Narasimha
    Jan 2 at 11:09











  • @Narasimha {"codes":{"200":"success"},{"401":"not found"}} Is it valid JSON ?

    – varatharajan
    Jan 2 at 11:25














3












3








3








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"}]}









share|improve this question
















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"}]}






java json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 11:13









Karol Dowbecki

24.8k93759




24.8k93759










asked Jan 2 at 10:20









NarasimhaNarasimha

1615




1615













  • this might be helpful stackoverflow.com/questions/20346790/…

    – Supun Dharmarathne
    Jan 2 at 10:35











  • Note that your desired output is not valid JSON

    – Hulk
    Jan 2 at 10:35






  • 1





    I think the reason you are finding it difficult to get the json you require is because it simply isn't valid json: {"codes":{"200":"success"},{"401":"not found"}}. Here you have codes object {"200":"success"} and then an anonymous object {"401":"not found"}

    – karen
    Jan 2 at 10:35











  • hi folks, desired json which I'm trying to generate will be valid one

    – Narasimha
    Jan 2 at 11:09











  • @Narasimha {"codes":{"200":"success"},{"401":"not found"}} Is it valid JSON ?

    – varatharajan
    Jan 2 at 11:25



















  • this might be helpful stackoverflow.com/questions/20346790/…

    – Supun Dharmarathne
    Jan 2 at 10:35











  • Note that your desired output is not valid JSON

    – Hulk
    Jan 2 at 10:35






  • 1





    I think the reason you are finding it difficult to get the json you require is because it simply isn't valid json: {"codes":{"200":"success"},{"401":"not found"}}. Here you have codes object {"200":"success"} and then an anonymous object {"401":"not found"}

    – karen
    Jan 2 at 10:35











  • hi folks, desired json which I'm trying to generate will be valid one

    – Narasimha
    Jan 2 at 11:09











  • @Narasimha {"codes":{"200":"success"},{"401":"not found"}} Is it valid JSON ?

    – varatharajan
    Jan 2 at 11:25

















this might be helpful stackoverflow.com/questions/20346790/…

– Supun Dharmarathne
Jan 2 at 10:35





this might be helpful stackoverflow.com/questions/20346790/…

– Supun Dharmarathne
Jan 2 at 10:35













Note that your desired output is not valid JSON

– Hulk
Jan 2 at 10:35





Note that your desired output is not valid JSON

– Hulk
Jan 2 at 10:35




1




1





I think the reason you are finding it difficult to get the json you require is because it simply isn't valid json: {"codes":{"200":"success"},{"401":"not found"}}. Here you have codes object {"200":"success"} and then an anonymous object {"401":"not found"}

– karen
Jan 2 at 10:35





I think the reason you are finding it difficult to get the json you require is because it simply isn't valid json: {"codes":{"200":"success"},{"401":"not found"}}. Here you have codes object {"200":"success"} and then an anonymous object {"401":"not found"}

– karen
Jan 2 at 10:35













hi folks, desired json which I'm trying to generate will be valid one

– Narasimha
Jan 2 at 11:09





hi folks, desired json which I'm trying to generate will be valid one

– Narasimha
Jan 2 at 11:09













@Narasimha {"codes":{"200":"success"},{"401":"not found"}} Is it valid JSON ?

– varatharajan
Jan 2 at 11:25





@Narasimha {"codes":{"200":"success"},{"401":"not found"}} Is it valid JSON ?

– varatharajan
Jan 2 at 11:25












2 Answers
2






active

oldest

votes


















1














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);





share|improve this answer


























  • hi Karol, It's same as finalJSON3 code snippet

    – Narasimha
    Jan 2 at 11:08











  • @Narasimha your desired output is not a valid JSON. You can craft it manually using StringBuilder but no sane JSON library will let you create {"codes":{"200":"success"},{"401":"not found"}}

    – Karol Dowbecki
    Jan 2 at 11:09





















0














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());





share|improve this answer
























  • output: finalJSON --> {"codes":{"200":{"description":"success"},"401":{"description":"failure"}}}

    – Narasimha
    Jan 3 at 8:15











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004574%2fhow-to-combine-two-or-more-json-objects-into-one-jsonobject%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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);





share|improve this answer


























  • hi Karol, It's same as finalJSON3 code snippet

    – Narasimha
    Jan 2 at 11:08











  • @Narasimha your desired output is not a valid JSON. You can craft it manually using StringBuilder but no sane JSON library will let you create {"codes":{"200":"success"},{"401":"not found"}}

    – Karol Dowbecki
    Jan 2 at 11:09


















1














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);





share|improve this answer


























  • hi Karol, It's same as finalJSON3 code snippet

    – Narasimha
    Jan 2 at 11:08











  • @Narasimha your desired output is not a valid JSON. You can craft it manually using StringBuilder but no sane JSON library will let you create {"codes":{"200":"success"},{"401":"not found"}}

    – Karol Dowbecki
    Jan 2 at 11:09
















1












1








1







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);





share|improve this answer















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);






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 11:10

























answered Jan 2 at 10:41









Karol DowbeckiKarol Dowbecki

24.8k93759




24.8k93759













  • hi Karol, It's same as finalJSON3 code snippet

    – Narasimha
    Jan 2 at 11:08











  • @Narasimha your desired output is not a valid JSON. You can craft it manually using StringBuilder but no sane JSON library will let you create {"codes":{"200":"success"},{"401":"not found"}}

    – Karol Dowbecki
    Jan 2 at 11:09





















  • hi Karol, It's same as finalJSON3 code snippet

    – Narasimha
    Jan 2 at 11:08











  • @Narasimha your desired output is not a valid JSON. You can craft it manually using StringBuilder but no sane JSON library will let you create {"codes":{"200":"success"},{"401":"not found"}}

    – Karol Dowbecki
    Jan 2 at 11:09



















hi Karol, It's same as finalJSON3 code snippet

– Narasimha
Jan 2 at 11:08





hi Karol, It's same as finalJSON3 code snippet

– Narasimha
Jan 2 at 11:08













@Narasimha your desired output is not a valid JSON. You can craft it manually using StringBuilder but no sane JSON library will let you create {"codes":{"200":"success"},{"401":"not found"}}

– Karol Dowbecki
Jan 2 at 11:09







@Narasimha your desired output is not a valid JSON. You can craft it manually using StringBuilder but no sane JSON library will let you create {"codes":{"200":"success"},{"401":"not found"}}

– Karol Dowbecki
Jan 2 at 11:09















0














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());





share|improve this answer
























  • output: finalJSON --> {"codes":{"200":{"description":"success"},"401":{"description":"failure"}}}

    – Narasimha
    Jan 3 at 8:15
















0














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());





share|improve this answer
























  • output: finalJSON --> {"codes":{"200":{"description":"success"},"401":{"description":"failure"}}}

    – Narasimha
    Jan 3 at 8:15














0












0








0







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());





share|improve this answer













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());






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 8:15









NarasimhaNarasimha

1615




1615













  • output: finalJSON --> {"codes":{"200":{"description":"success"},"401":{"description":"failure"}}}

    – Narasimha
    Jan 3 at 8:15



















  • output: finalJSON --> {"codes":{"200":{"description":"success"},"401":{"description":"failure"}}}

    – Narasimha
    Jan 3 at 8:15

















output: finalJSON --> {"codes":{"200":{"description":"success"},"401":{"description":"failure"}}}

– Narasimha
Jan 3 at 8:15





output: finalJSON --> {"codes":{"200":{"description":"success"},"401":{"description":"failure"}}}

– Narasimha
Jan 3 at 8:15


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004574%2fhow-to-combine-two-or-more-json-objects-into-one-jsonobject%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith