1

I am trying to parse nested Json with duplicate keys in Java . I've went through other similar questions but am unable to find a solution.I tried an approach using the Jackson streaming API but it only prints the value of the first key and ignores the second duplicate key. Please help.Thanks in advance.

Code:

public class JacksonJson {
    public static void main(String args[]) throws IOException {


        /
        JsonFactory factory = new JsonFactory();

        JsonParser parser = factory.createParser(new File("a.json"));
        parser.nextToken();                                
        while (parser.nextToken() != JsonToken.END_OBJECT) {    //loop until "}"

            String fieldName = parser.getCurrentName();

            if (fieldName.equals("A")) {
                parser.nextToken();
                System.out.println("Value : " + parser.getText());
            }
             /*else { // unexpected token, generate error
                throw new IOException("Unrecognized field '"+fieldName+"'");
            }*/

        }
        parser.close();
    }
    }

Json file:

{
  "Data": {
    "C": {
      "S": {
        "M": {},
        "A": "first"
      }
    },
    "C": {
      "S": {
        "M": {}
        "A": "Second",

      }
    }
  }
}
Vinayak B
  • 4,430
  • 4
  • 28
  • 58
IrMan
  • 11
  • 1
  • 3
  • 1
    see https://stackoverflow.com/questions/21832701/does-json-syntax-allow-duplicate-keys-in-an-object?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Jens May 29 '18 at 07:49
  • This is not a "valid JSON" because of that duplicated key for most of the API... strangely though, this is not define in the standard format for JSON – AxelH May 29 '18 at 07:49
  • 1
    it is not recommended to have duplicate keys in json, if you tryto get the value based on the key it will give the second one – Derrick May 29 '18 at 07:50
  • See [Parsing a json which contains duplicate keys](https://stackoverflow.com/questions/42573120/parsing-a-json-which-contains-duplicate-keys). –  May 29 '18 at 07:54
  • If you're using the streaming api, you must be ready to parse completely any JSON structure, and deal with nested structures. – Maurice Perry May 29 '18 at 07:55
  • It do seems like a duplicate candidate @saka1029 ;) – AxelH May 29 '18 at 07:55
  • 1
    Possible duplicate of [Parsing a json which contains duplicate keys](https://stackoverflow.com/questions/42573120/parsing-a-json-which-contains-duplicate-keys) – Rcordoval May 29 '18 at 08:10
  • I know it's not recommended to have duplicates but since the json is externally generated,I have to parse it in its current format. – IrMan May 29 '18 at 08:38

2 Answers2

1

From the JSON specification

A name is a string. A single colon comes after each name, separating the name from the value. A single comma separates a value from a following name. The names within an object SHOULD be unique.

Many JSON libraries offer the functionality to map the json to an object. Think about it, how should they even work, if the names aren't unique?

Of course, you could try to parse it yourself and rename it internally to C1, C2, ... but it's far easier to disallow such cases.

Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60
Shryne
  • 121
  • 5
  • 1
    Even if this is interesting, this doesn't answer the problem. _SHOULD_ doesn't technically forbid it. If this was the question, this would be a duplicate of [Does JSON syntax allow duplicate keys in an object?](https://stackoverflow.com/a/38267020/4391450) – AxelH May 29 '18 at 07:53
  • 1
    The json is externally generated so I can't touch it. – IrMan May 29 '18 at 08:36
0

Use below code for getting json value from duplicate entries.

public static void main(String args[]) throws IOException {     
    JsonFactory factory = new JsonFactory();
    JsonParser parser = factory.createJsonParser(new File("src/a.json"));
    parser.nextToken();  
    int count=0;
    while (parser.nextToken() != JsonToken.NOT_AVAILABLE) {    //loop until "}"
        String fieldName = parser.getCurrentName();

        if(fieldName==null){
            break;
        }
        if(fieldName.equals("C")&&parser.getText().equals("C")){
            count++;
        }
        if (fieldName.equals("A")) {
            parser.nextToken();
            System.out.println("Value : " + parser.getText());
        }
    }
    System.out.println(count);
    parser.close();
}

And also your json string is not valid. Use below valid string

 {
  "Data": {
    "C": {
      "S": {
        "M": {},
        "A": "first"
      }
    },
    "C": {
      "S": {
        "M": {},
        "A": "Second"

      }
    }
  }
}
Vinayak B
  • 4,430
  • 4
  • 28
  • 58