3

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"}}]}}

2 Answers 2

4

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 );
Sign up to request clarification or add additional context in comments.

1 Comment

I am using that.But instead of hardcoding it,I want to read it from a file.When I do that I am unable to achieve the same result.
0

You need to stringify your JSON object

a.toString()

3 Comments

Still same result.
JSON works with a key - value approach. Keys are always unique, so it doesn't make sense to have duplicate keys.
I know but I have to work with a json with duplicate keys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.