JSON allows duplicates keys but Javascript Objects
do not.
Because keys can be duplicated, that means the JSON cannot be represented as key/value based dictionary and cannot be converted directly to a Javascript Object
.
You will have to "walk" through each key/value pair in the JSON and then process it as you see fit. I don't know what structure you are looking to convert into, but it as long as it's not a Javascript Object
, then it's fine. For example, you might walk through a JSON object and output the values to an .ini
or .css
file where "keys" can be repeated.
JSON.parse()
will not work because that will try to parse into a Javascript Object
, which does not allow duplicated keys. The solution there is to simply overwrite any previous value if the key is reused. But, we can use code based on how JSON.parse()
works and instead of setting properties on a Javascript Object
, you can split from there and apply your custom solution.
You can modify a complete JSON parser like json2 or use parsers that stream keys as they arrive like clarinet or Oboe.js.