Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

merging two json objects into single object in java

Tags:

java

json

I have two json objects like below:

{"name":["Karbonn Smart A12 Star (Black & Silver)","Nokia 220 (Black)","Karbonn Smart A52 Plus (Black & Gold)","Karbonn Smart A12 Star (White & Gold)",.......]}
{"price":["Rs. 3,699","Rs. 2,599","Rs. 2,499","Rs. 3,699",..........]}

I would like to combine both the objects like below I tried by using nested for each loop it did not worked I am not knowing the procedure to achieve this:

{"mobile":[{"name":"Karbonn Smart A12 Star (Black & Silver)","price":"Rs. 2,499"}]...........}

my code is below:

for(Element a:mobilename)
    {
    text= a.text();
    arr.add(text);
    obj1.put("name", arr);
    //a11.add(text);

}
   arr2.add(obj1);

    for(Element b:price)
    {
    text1=b.text();
    arr1.add(text1);

    obj.put("price", arr1);




     }
    arr2.add(obj1);
    arr2.add(obj);
    obj2.put("mobile", arr2);
like image 722
Raj Avatar asked Dec 13 '14 10:12

Raj


People also ask

How to merge two JSON arrays in Java?

A JSON is a lightweight data-interchange format and the format of JSON is a key with value pair. The JSONArray can parse text from a String to produce a vector-like object and supports java.util.List interface. We can use org.json.simple.JSONArray class to merge two JSON arrays in Java.

How do I merge multiple keys in a JSON object?

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

What is the use of merging in JSON?

Note however that the merging operation is useful outside the scope of JSON processing. It might also be useful for some JavaScript applications where we simply want to merge two JavaScript objects, without actually doing any deserialization / serialization of JSON strings.

What is the difference between JSON and jsonobject?

A JSON is a lightweight data-interchange format and the format of JSON is a key with value pair. The JSONObject can parse a text from a String to produce a map-like object and supports java.util.Map interface.


2 Answers

You can iterate via for loop and in every iteration create new JSONObject and add it to a collection. Finally add the collection to the mergedObject. E.g.

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collection;

public class Foo {
    public static void main(String[] args) throws JSONException {

        JSONObject object1 = new JSONObject("{\n" +
                "    \"name\": [\n" +
                "        \"Karbonn Smart A12 Star (Black & Silver)\",\n" +
                "        \"Nokia 220 (Black)\",\n" +
                "        \"Karbonn Smart A52 Plus (Black & Gold)\",\n" +
                "        \"Karbonn Smart A12 Star (White & Gold)\",\n" +
                "        \"Karbonn Smart A50s (Black)\",\n" +
                "        \"Karbonn Smart A52 Plus (White & Silver)\",\n" +
                "        \"Karbonn Titanium S2 Plus (White)\",\n" +
                "        \"Karbonn Smart A11 Star (Black)\",\n" +
                "        \"Karbonn Smart A11 Star (White)\"\n" +
                "    ]\n" +
                "}");

        JSONObject object2 = new JSONObject("{\n" +
                "    \"price\": [\n" +
                "        \"Rs. 3,699\",\n" +
                "        \"Rs. 2,599\",\n" +
                "        \"Rs. 2,499\",\n" +
                "        \"Rs. 3,699\",\n" +
                "        \"Rs. 2,699\",\n" +
                "        \"Rs. 2,499\",\n" +
                "        \"Rs. 4,999\",\n" +
                "        \"Rs. 4,399\",\n" +
                "        \"Rs. 4,499\"\n" +
                "    ]\n" +
                "}");


        JSONArray nameArray = object1.getJSONArray("name");
        JSONArray priceArray = object2.getJSONArray("price");

        JSONObject mergedObject = new JSONObject("{}");
        Collection<JSONObject> collection = new ArrayList<>();

        for (int i = 0; i < nameArray.length(); i++) {
            JSONObject obj = new JSONObject();
            obj.put("name", nameArray.getString(i));
            obj.put("price", priceArray.getString(i));
            collection.add(obj);
        }

        mergedObject.put("mobile", collection);
        System.out.println(mergedObject);
    }
}

Ouput:

{"mobile":[{"price":"Rs. 3,699","name":"Karbonn Smart A12 Star (Black & Silver)"},{"price":"Rs. 2,599","name":"Nokia 220 (Black)"},{"price":"Rs. 2,499","name":"Karbonn Smart A52 Plus (Black & Gold)"},{"price":"Rs. 3,699","name":"Karbonn Smart A12 Star (White & Gold)"},{"price":"Rs. 2,699","name":"Karbonn Smart A50s (Black)"},{"price":"Rs. 2,499","name":"Karbonn Smart A52 Plus (White & Silver)"},{"price":"Rs. 4,999","name":"Karbonn Titanium S2 Plus (White)"},{"price":"Rs. 4,399","name":"Karbonn Smart A11 Star (Black)"},{"price":"Rs. 4,499","name":"Karbonn Smart A11 Star (White)"}]}
like image 66
sol4me Avatar answered Oct 18 '22 23:10

sol4me


you can find org.json here JSON-java

import java.util.ArrayList;
import java.util.List;
import org.json.*;

public class JsonTest
{   
    public String mergeJson(String name, String price)
    {
        JSONObject nameJ= new JSONObject(name);
        JSONObject priceJ= new JSONObject(price);
        JSONObject mobileJ = new JSONObject();

        JSONArray names = nameJ.getJSONArray("name");
        JSONArray prices = priceJ.getJSONArray("price");
        JSONArray mobiles = new JSONArray();

        if(names.length() == prices.length())
        {
            for(int i=0;i<names.length();i++)
            {
                JSONObject mobile = new JSONObject();
                mobile.put("name", names.getString(i));
                mobile.put("price", prices.getString(i));
                mobiles.put(mobile);
            }
        }

        mobileJ.put("mobile", mobiles);

        return mobileJ.toString();          
    }

    public static void main(String[] args)
    {
        String name = "{'name':['Karbonn Smart A12 Star (Black & Silver)','Nokia 220 (Black)','Karbonn Smart A52 Plus (Black & Gold)','Karbonn Smart A12 Star (White & Gold)','Karbonn Smart A50s (Black)','Karbonn Smart A52 Plus (White & Silver)','Karbonn Titanium S2 Plus (White)','Karbonn Smart A11 Star (Black)','Karbonn Smart A11 Star (White)']}";
        String price = "{'price':['Rs. 3,699','Rs. 2,599','Rs. 2,499','Rs. 3,699','Rs. 2,699','Rs. 2,499','Rs. 4,999','Rs. 4,399','Rs. 4,499']}";

        JsonTest test = new JsonTest();
        System.out.println(test.mergeJson(name,price));
    }

}
like image 2
neohope Avatar answered Oct 19 '22 01:10

neohope