question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Gson does not detect duplicate keys in the nested json

See original GitHub issue

Gson is unable detect duplicate keys in the nested json

See below code snippet

public static void main(String[] args) throws IOException {
    Map<String, ?> map;
    //this does not detect duplicate 'key'
    map = new Gson().fromJson("{'a':'x','aa':{'key':'x','key':'y'}}", Map.class);

    //As expected , this call fails with  Exception in thread "main" com.google.gson.JsonSyntaxException: duplicate key: a
    map = new Gson().fromJson("{'a':'x','a':'y'}", Map.class);
}

Is this the expected behavior ? If so is there any alternative to fail in the first case as well?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:10

github_iconTop GitHub Comments

11reactions
pratapkishorevarmacommented, Jan 5, 2017

Please add a option, which can be set to not allow duplicate keys.

0reactions
CsabaMiomnicommented, Mar 30, 2022

+1 for the option

Don’t have any hope, Google is against the devs. Again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Make Gson throw exception on parsing JSON with duplicated ...
I want it to throw some exception. I know I can parse JSON as map, and then Gson throws exception in such case,...
Read more >
Gson does not detect duplicate keys in the nested json
Gson is unable detect duplicate keys in the nested json. See below code snippet public static void main(String[] args) throws IOException ...
Read more >
Parsing between JSON and Kotlin Object with Google Gson ...
In this tutorial I will discuss about how to parse a class object into json string and convert it back from json string...
Read more >
An Exploration of JSON Interoperability Vulnerabilities
Even within the official JSON RFC, there is open-ended guidance on a few topics, such as how to handle duplicate keys and represent...
Read more >
How do you handle duplicate keys in JSON? - Quora
No. A map cannot have duplicate keys. You access the value associated with the unique key using operator[] (i.e., square brackets). You would...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found