-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Duplicate object keys shouldn't result in an ArgumentException #71784
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionCurrently, when a parsed JSON object has duplicate keys, an Reproduction Stepsvar json = JsonNode.Parse("[{\"foo\":1, \"bar\":2, \"foo\":3}]") as JsonArray;
var item = json[0]["bar"]; Expected behaviorMaybe throw Actual behavior
Regression?No response Known WorkaroundsNo response ConfigurationSeen in all released versions (.Net 6 and previous) Other informationNo response
|
since this is easy enough let's try to fix this in 7.0 |
I'm removing the |
There are some third-party webapi that return json with duplicate keys, which is allowed in the Newtonsoft.Json. Now using the following code reflection modification fixes the problem, but will the November .NET 8 RTM fix it?
|
Unfortunately, no. Development for .NET 8 concluded back in August and any work happening since targets .NET 9. |
Hi @gregsdennis, |
From Eirik's comment above, I would guess not. |
I just ran into this. In my case, I get the json from some web api. Merely calling Count on the JsonObject, or iterating through its child properties, results in the ArgumentException... Is there a workaround to either allow multiple entries with the same name, allow the caller to rename duplicate entries, or at least ignore re-definitions or something? As it is, the current behavior presents a problem to adopt System.Text.Json. |
We would be happy to accept a PR that addresses the issue, since we can't prioritise a fix for the moment. |
Description
Currently, when a parsed JSON object has duplicate keys, an
ArguementException
is thrown from the underlying dictionary implementation. I would expect this to be thrown if I were manually attempting to add an existing key, but not from parsing.Reproduction Steps
Expected behavior
Maybe throw
JsonException
?Actual behavior
ArgumentException
is thrown. Stack trace:Regression?
No response
Known Workarounds
No response
Configuration
Seen in all released versions (.Net 6 and previous)
Other information
No response
The text was updated successfully, but these errors were encountered: