-1

I know this has probably been asked a million times but I spent the whole day and I cannot resolve my problem. So here it is. I have a json object in the following format.

So I am trying to loop through this json object but just cannot be able to do. I simply can get iy working. For example:

const json = [
  [{
    "Id": 1,
    "Name": "Jeffreys Bay",
    "ProvinceID": 1
  }, {
    "Id": 2,
    "Name": "Heidelberg",
    "ProvinceID": 3
  }]
]

for (let x in json) {
  console.log(x + ': ' + json[x]);
}

returns

core.js:6479 ERROR SyntaxError: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)
    at SafeSubscriber._next (location.page.ts:71)
    at SafeSubscriber.__tryOrUnsub (Subscriber.js:183)
    at SafeSubscriber.next (Subscriber.js:122)
    at Subscriber._next (Subscriber.js:72)
    at Subscriber.next (Subscriber.js:49)
    at MapSubscriber._next (map.js:35)
    at MapSubscriber.next (Subscriber.js:49)
    at FilterSubscriber._next (filter.js:33)
    at FilterSubscriber.next (Subscriber.js:49)

Please help me??

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Daantjiekat
  • 29
  • 1
  • 4
  • 7
    [There is no JSON anywhere in your question](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – Quentin Aug 31 '21 at 15:03
  • 1
    Your code does not throw the error you say it does – Quentin Aug 31 '21 at 15:04
  • What you have is an array of arrays of objects. And [your loop produces no error](https://jsfiddle.net/oktxeycn/). Where is there any JSON being parsed? – David Aug 31 '21 at 15:05
  • The object is an array inside an array, either make it a simple array, or run two loops inside of each other. – tsamridh86 Aug 31 '21 at 15:06
  • You have double [ ] try to remove one. You got object of object there. – Patfreeze Aug 31 '21 at 15:06

1 Answers1

0

Try to do this:

const json = [
  [{
    "Id": 1,
    "Name": "Jeffreys Bay",
    "ProvinceID": 1
  }, {
    "Id": 2,
    "Name": "Heidelberg",
    "ProvinceID": 3
  }]
]

for (let x in json) {
  console.log(x + ': ' + JSON.stringify(json[x]));
}
J_K
  • 688
  • 5
  • 12