I am consuming some JSON from two different sources, I end up with two JSONObject
s 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 put
s?
JSONObject to merge two JSON objects in Java. We can merge two JSON objects using the putAll() method (inherited from interface java. util.
simple. JSONArray class to merge two JSON arrays in Java. We can merge two JSON arrays using the addAll() method (inherited from interface java.
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' ...}]
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.
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"}} }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With