-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??

5
  • 7
    There is no JSON anywhere in your question Commented Aug 31, 2021 at 15:03
  • 1
    Your code does not throw the error you say it does Commented Aug 31, 2021 at 15:04
  • What you have is an array of arrays of objects. And your loop produces no error. Where is there any JSON being parsed? Commented Aug 31, 2021 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. Commented Aug 31, 2021 at 15:06
  • You have double [ ] try to remove one. You got object of object there. Commented Aug 31, 2021 at 15:06

1 Answer 1

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]));
}

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

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.