Q:

How to remove duplicate objects from json array using hashset?

Any one help me out from this issue,
How to remove duplicate objects from JSON using JAVA hash set? Here I want to remove duplicates from below JSON
{
"others":[
    {
        "id":"1",
        "caption":"test"
    },
    {
        "id":"2",
        "caption":"self"
    },
    {
        "id":"2",
        "caption":"Self"
    },
    {
        "id":"1",
        "caption":"test"
    }
],
"quantity":[
    {
        "id":"1",
        "caption":"self1"
    },
    {
        "id":"1",
        "caption":"self1"
    }
]}

A:

Below code parses your json and puts it in the HashMap to get unique elements
    HashMap<String,String> hashOut = new HashMap<String,String>();
    String input = "{\"others\":[    {        \"id\":\"1\",        \"caption\":\"test\"    },    {        \"id\":\"2\",        \"caption\":\"self\"    },    {        \"id\":\"2\",        \"caption\":\"self\"    },    {        \"id\":\"1\",        \"caption\":\"test\"    }],\"quantity\":[    {        \"id\":\"1\",        \"caption\":\"self1\"    },    {        \"id\":\"1\",        \"caption\":\"self1\"    }]}";
    JSONObject jsonObj = new JSONObject(input);
    Iterator it = jsonObj.keys();

    while (it.hasNext()) {
        hashOut.clear();
        JSONArray jsInner = (JSONArray) jsonObj.getJSONArray((String) it.next());
        for (int i=0;i<jsInner.length();i++) {
            JSONObject jo = new JSONObject(jsInner.get(i).toString());
            hashOut.put((String)jo.get("id"), (String)jo.get("caption"));
        }
        System.out.println(hashOut);
    }

To get the output as below
{2=self, 1=test}
{1=self1}

