1

I have a test nested JSON string.

const testString = `{
  "object1": {
    "5": [
      {
        "id": "A2OKPZ5S9F78PD",
        "rate": "2",
        "item": "item",
        "status": "status"
      }
    ]
  },
  "type": "LIVE_EVENT"
}`;

const model = JSON.parse(testString);
Object.values(model.object1).forEach((obj) =>
  obj.foreach((innerObj) => console.log(innerObj))
);

As you can see above I am trying to parse this as JSON and iterate over. The problem which I am facing during JSON.parse the inner object assumes the type undefined and foreach can not be applied on it. Can some one please help ?

Luís Ramalho
  • 10,018
  • 4
  • 52
  • 67

1 Answers1

1

Your JSON is was invalid (before an edit) due to an extra comma after the status key/value pair and forEach() has an uppercase E. Also, as discussed in the comments below it seems you need to cast the inner obj to be of a type that understands forEach():

const testString = `{"object1":{"5":[{"id":"A2OKPZ5S9F78PD","rate":"2","item":"item","status":"status"}]},"type":"LIVE_EVENT"}`;
const model = JSON.parse(testString);
Object.values(model.object1).forEach((obj) =>
  (obj as any).forEach((innerObj) => console.log(innerObj))
);
Luís Ramalho
  • 10,018
  • 4
  • 52
  • 67