Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge (Concat) Multiple JSONObjects in Java

Tags:

java

json

concat

I am consuming some JSON from two different sources, I end up with two JSONObjects and I'd like to combine them into one.

Data:

"Object1": {     "Stringkey":"StringVal",     "ArrayKey": [Data0, Data1] }  "Object2": {     "Stringkey":"StringVal",     "Stringkey":"StringVal",     "Stringkey":"StringVal", } 

Code, using http://json.org/java/ library:

// jso1 and jso2 are some JSONObjects already instantiated JSONObject Obj1 = (JSONObject) jso.get("Object1"); JSONObject Obj2 = (JSONObject) jso.get("Object2"); 

So in this situation I'd like to combine Obj1 and Obj2, either to make a totally new JSONObject or concat one to the other. Any ideas besides pulling them all apart and individually adding in by puts?

like image 947
DanInDC Avatar asked Mar 08 '10 17:03

DanInDC


People also ask

How do I combine multiple JSON objects into one?

JSONObject to merge two JSON objects in Java. We can merge two JSON objects using the putAll() method (inherited from interface java. util.

How do I add two JSON arrays in Java?

simple. JSONArray class to merge two JSON arrays in Java. We can merge two JSON arrays using the addAll() method (inherited from interface java.

How do I combine two JSON objects with the same key?

Use concat() for arrays Assuming that you would like to merge two JSON arrays like below: var json1 = [{id:1, name: 'xxx' ...}] var json2 = [{id:2, name: 'xyz' ...}]


2 Answers

If you want a new object with two keys, Object1 and Object2, you can do:

JSONObject Obj1 = (JSONObject) jso1.get("Object1"); JSONObject Obj2 = (JSONObject) jso2.get("Object2"); JSONObject combined = new JSONObject(); combined.put("Object1", Obj1); combined.put("Object2", Obj2); 

If you want to merge them, so e.g. a top level object has 5 keys (Stringkey1, ArrayKey, StringKey2, StringKey3, StringKey4), I think you have to do that manually:

JSONObject merged = new JSONObject(Obj1, JSONObject.getNames(Obj1)); for(String key : JSONObject.getNames(Obj2)) {   merged.put(key, Obj2.get(key)); } 

This would be a lot easier if JSONObject implemented Map, and supported putAll.

like image 98
Matthew Flaschen Avatar answered Sep 25 '22 00:09

Matthew Flaschen


In some cases you need a deep merge, i.e., merge the contents of fields with identical names (just like when copying folders in Windows). This function may be helpful:

/**  * Merge "source" into "target". If fields have equal name, merge them recursively.  * @return the merged object (target).  */ public static JSONObject deepMerge(JSONObject source, JSONObject target) throws JSONException {     for (String key: JSONObject.getNames(source)) {             Object value = source.get(key);             if (!target.has(key)) {                 // new value for "key":                 target.put(key, value);             } else {                 // existing value for "key" - recursively deep merge:                 if (value instanceof JSONObject) {                     JSONObject valueJson = (JSONObject)value;                     deepMerge(valueJson, target.getJSONObject(key));                 } else {                     target.put(key, value);                 }             }     }     return target; }    /**  * demo program  */ public static void main(String[] args) throws JSONException {     JSONObject a = new JSONObject("{offer: {issue1: value1}, accept: true}");     JSONObject b = new JSONObject("{offer: {issue2: value2}, reject: false}");     System.out.println(a+ " + " + b+" = "+JsonUtils.deepMerge(a,b));     // prints:     // {"accept":true,"offer":{"issue1":"value1"}} + {"reject":false,"offer":{"issue2":"value2"}} = {"reject":false,"accept":true,"offer":{"issue1":"value1","issue2":"value2"}} } 
like image 22
Erel Segal-Halevi Avatar answered Sep 26 '22 00:09

Erel Segal-Halevi