17

What is the best way to combine (merge) two JSONObjects?

JSONObject o1 = {
    "one": "1",
    "two": "2",
    "three": "3"
}
JSONObject o2 = {
        "four": "4",
        "five": "5",
        "six": "6"
    }

And result of combining o1 and o2 must be

JSONObject result = {
        "one": "1",
        "two": "2",
        "three": "3",
        "four": "4",
        "five": "5",
        "six": "6"
    }
jimpanzer
  • 3,470
  • 4
  • 44
  • 84

5 Answers5

14

I have your same problem: I can't find the putAll method (and it isn't listed in the official reference page).

So, I don't know if this is the best solution, but surely it works quite well:

//I assume that your two JSONObjects are o1 and o2
JSONObject mergedObj = new JSONObject();

Iterator i1 = o1.keys();
Iterator i2 = o2.keys();
String tmp_key;
while(i1.hasNext()) {
    tmp_key = (String) i1.next();
    mergedObj.put(tmp_key, o1.get(tmp_key));
}
while(i2.hasNext()) {
    tmp_key = (String) i2.next();
    mergedObj.put(tmp_key, o2.get(tmp_key));
}

Now, the merged JSONObject is stored in mergedObj

Vito Gentile
  • 13,336
  • 9
  • 61
  • 96
3

json objects to be merge in that new json object like this.

    JSONObject jObj = new JSONObject();
    jObj.put("one", "1");
    jObj.put("two", "2");
    JSONObject jObj2 = new JSONObject();
    jObj2.put("three", "3");
    jObj2.put("four", "4");


    JSONParser p = new JSONParser();
    net.minidev.json.JSONObject o1 = (net.minidev.json.JSONObject) p
                        .parse(jObj.toString());
    net.minidev.json.JSONObject o2 = (net.minidev.json.JSONObject) p
                        .parse(jObj2.toString());

    o1.merge(o2);

    Log.print(o1.toJSONString());

now o1 will be the merged json object. you will get the output like this ::

{"three":"3","two":"2","four":"4","one":"1"}

please refer this link and download the smartjson library ..here is the link http://code.google.com/p/json-smart/wiki/MergeSample

hope it will help.

Nirav Tukadiya
  • 3,367
  • 1
  • 18
  • 36
  • I don't believe doing it that way would give the output required in the OP – TMH Oct 24 '13 at 12:49
  • @Neurenor - that way will give the wrong output. see question. – jimpanzer Oct 24 '13 at 13:22
  • Thanks for your answer, but I do not want to include in my project third-party libraries for such small operations. – jimpanzer Oct 25 '13 at 07:26
  • i have googled it and find this thing.it is easier to use.may be you will implement the code for this but they already have implemented and there is nothing bad in using library if it saves your time. – Nirav Tukadiya Oct 25 '13 at 08:11
0

How about this:

            Iterator iterator = json2.keys();
            while(iterator.hasNext()){
                String key = iterator.next().toString();
                json1.put(key,map.optJSONObject(key));
            }
tainy
  • 951
  • 7
  • 19
0

Merge JsonObject(gson)-

JsonObject data = new JsonObject();
data = receivedJsoData.get("details").getAsJsonObject();

JsonObject data2 = new JsonObject();
data2 = receivedJsoData1.get("details").getAsJsonObject();

JsonObject mergedData = new JsonObject();

Set<Map.Entry<String, JsonElement>> entries = data1.entrySet();  //will return members of your object
for (Map.Entry<String, JsonElement> entry : entries) {
    mergedData.add(entry.getKey(), entry.getValue());
}
Set<Map.Entry<String, JsonElement>> entries1 = data2.entrySet();  //will return members of your object
for (Map.Entry<String, JsonElement> entry : entries1) {
    mergedData.add(entry.getKey(), entry.getValue());
}
Abhishek singh
  • 415
  • 7
  • 20
-1

Try this.. hope it helps

JSONObject result = new JSONObject();
result.putAll(o1);
result.putAll(O2);
Ankur
  • 140
  • 2
  • 1
    Sorry, but I can't find "putAll" method in org.json.JSONObject [Source](http://developer.android.com/reference/org/json/JSONObject.html) – jimpanzer Oct 24 '13 at 13:24
  • putAll will replace all the current mappings which is in the result jsonobject, meaning, when o1 object is put and then the o2 object is also put, o1 would be replaced and only o2 will remain in result object – Kripa Jayakumar Apr 27 '15 at 20:58