0

I have a API response, which is like the below code

const res = { "a" : {}, "a" : {} } 
7
  • 4
    I don't think this is valid JSON. Commented Jun 10, 2022 at 14:01
  • 1
    This is invalid json. Error: Duplicate key 'a' will be shown Commented Jun 10, 2022 at 14:02
  • Maybe related: stackoverflow.com/q/40956189/691711 Commented Jun 10, 2022 at 14:02
  • I know this is a invalid JSON. But is there any way to iterate it ? Commented Jun 10, 2022 at 14:04
  • Can you possibly make the key unique before parsing the JSON? Commented Jun 10, 2022 at 14:05

2 Answers 2

1

This is not possible. In JSON, there is no error if you use the same name for multiple keys, but the last key with the same name will be used. I suggest using an array for the values for a key.

E.g.:

const res = { "a" : {}, "a" : {} } 

would be

const res = { "a" : [{}, {}] } 

Then you could iterate on the list.

Sign up to request clarification or add additional context in comments.

Comments

0

if "a" isn't used to identify it's value, it shouldn't be a key. You could restructure you JSON to look like this:

const res = [ ["a", {}], ["a", {}] ] 

and then iterate over it using:

for(let [k,v] of res) print(k,v) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.