0

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

const res = {

"a" : {},
"a" : {}

}
Naveen Kumar
  • 1,476
  • 5
  • 28
  • 52

2 Answers2

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.

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)
LoLucky
  • 103
  • 4