How detect duplicate keys in JSON?

kdevel kdevel at vogtner.de
Thu Dec 17 21:01:30 UTC 2020


~~~json.d
void main ()
{
    import std.stdio;
    import std.json;

    string [] json = [
`{
    "range": [
       {"x": "iks"}
    ],
    "range": "variable"
}`,
`{
    "range": "variable",
    "range": [
       {"x": "iks"}
    ]
}`
    ];
    foreach (js; json) {
       auto jv = parseJSON (js, JSONOptions.strictParsing);
       writeln ("json = ", js);
       writeln ("JSONValue = ", jv);
    }
}
~~~

$ ./json
json = {
    "range": [
       {"x": "iks"}
    ],
    "range": "variable"
}
JSONValue = {"range":"variable"}
json = {
    "range": "variable",
    "range": [
       {"x": "iks"}
    ]
}
JSONValue = {"range":[{"x":"iks"}]}

There has been some discussion (TL;DR) in this forum [1] about
this topic. The doc page [2] keeps silent but suggests using the
strictParsing flag which enables to "Strictly follow RFC-8259 
grammar
when parsing" which unfortunately does not help here.

[1] 
http://forum.dlang.org/thread/nbuhouhimowvcqssvfnr@forum.dlang.org?page=14#post-mqlj4s:242497:241:40digitalmars.com
[2] https://dlang.org/phobos/std_json.html


More information about the Digitalmars-d-learn mailing list