2

I am trying to parse Json file which contains duplicate keys in java.I am using the method suggesteed 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.Please Help as I want to read the Json from a file.Thanks in advance. 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"}}]}}
sman001
  • 21
  • 2

2 Answers2

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 );
Ravi Sapariya
  • 395
  • 2
  • 11
  • 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. – sman001 May 29 '18 at 11:36
0

You need to stringify your JSON object

a.toString()
higz555
  • 115
  • 8