sman001
sman001

Reputation: 31

Parsing json with duplicate keys

I am trying to parse JSON file which contains duplicate keys in Java. I am using the method suggested in this answer Parsing a json which contains duplicate keys.

The approach works fine if I hard-code the JSON but if I read it from a file,only the last duplicate key is read.

Code:

public class am {
    public static  void main(String args[]) throws Exception
    {
        JSONParser parser = new JSONParser();


        Object obj = parser.parse(new FileReader("sample.json"));

        JSONObject a=JSONObject.fromObject(obj);
        JSONObject jsonObject=JSONObject.fromObject("{\n" +
                "  \"Data\": {\n" +
                "    \"A\": {\n" +
                "      \"B\": {\n" +
                "        \"C\": \"c\",\n" +
                "        \"D\": {}\n" +
                "      },\n" +
                "      \"E\": {\n" +
                "        \"F\": \"f\"\n" +
                "      },\n" +
                "      \"G\": {\n" +
                "        \"H\": \"h\"\n" +
                "      }\n" +
                "    },\"A\": {\n" +
                "      \"B\": {\n" +
                "        \"C\": \"x\",\n" +
                "        \"D\": {}\n" +
                "      },\n" +
                "      \"E\": {\n" +
                "        \"F\": \"y\"\n" +
                "      },\n" +
                "      \"G\": {\n" +
                "        \"H\": \"z\"\n" +
                "      }\n" +
                "    },\n" +
                "    
                "\n" +
                "  }\n" +
                "}");
        System.out.println("Json objects are:::"+a);
        System.out.println("Json objects are:::"+jsonObject);
       
    }
}

Json File:

{
  "Data": {
    "A": {
      "B": {
        "C": "c",
        "D": {}
      },
      "E": {
        "F": "f"
      },
      "G": {
        "H": "h"
      }
    },"A": {
      "B": {
        "C": "x",
        "D": {}
      },
      "E": {
        "F": "y"
      },
      "G": {
        "H": "z"
      }
    },
    
  }
}

Output:

Json objects are:::{"Data":{"A":{"B":{"C":"x","D":{}},"E":{"F":"y"},"G":{"H":"z"}}}}
Json objects are:::{"Data":{"A":[{"B":{"C":"c","D":{}},"E":{"F":"f"},"G":{"H":"h"}},{"B":{"C":"x","D":{}},"E":{"F":"y"},"G":{"H":"z"}}]}}

Upvotes: 3

Views: 2075

Answers (2)

Ravi Sapariya
Ravi Sapariya

Reputation: 405

If you do not have a constraint to use external JSON library than you can use net.sf.json.JSONObject to parse your JSON string, It will accept JSON with the duplicate key. It will retain the duplicated values by storing them into arrays.

JSONObject jsonObject = JSONObject.fromObject( "{\"Data\": {\"A\": {\"B\": {\"C\": \"c\",\"D\": {}},\"E\": {\"F\": \"f\"},\"G\": {\"H\": \"h\"}},\"A\": {\"B\": {\"C\": \"x\",\"D\": {}},\"E\": {\"F\": \"y\"},\"G\": {\"H\": \"z\"}}}}" );
System.out.println( "net.sf.json.JSONObject: " + jsonObject );

Upvotes: 4

higz555
higz555

Reputation: 140

You need to stringify your JSON object

a.toString()

Upvotes: 0

Related Questions