1

The Nested JSON - image

{"newsalert":[{"newslist":{"1":{"newsid" :"4321","headline" :"Great White Shark Found","newscode" :"GWS","newstime" :"10:04:32"},"2":{"newsid" :"8031","headline" :"Polar Bear Escaped","newscode" :"PBE","newstime" :"09:28:03"}}}]}


var dt = JSON.parse(json_string);
var value = dt.newsalert[0].newslist.headline;
console.log(value);  // Undefined result

I am having a hard time figuring out how to extract the newslist which is inside the array newsalert. Need to extract the properties newsid, headline, newscode.

Thank you :)

ernie
  • 33
  • 5
  • Possible duplicate of [JavaScript object: access variable property by name as string](https://stackoverflow.com/questions/4255472/javascript-object-access-variable-property-by-name-as-string) – kemicofa ghost Sep 29 '19 at 14:55

1 Answers1

1

Dont know exactly what you are trying to achieve, but this is how you access the properties in json

var a={
  "newsalert": [{
    "newslist": {
      "1": {
        "newsid": "4321",
        "headline": "Great White Shark Found",
        "newscode": "GWS",
        "newstime": "10:04:32"
      },
      "2": {
        "newsid": "8031",
        "headline": "Polar Bear Escaped",
        "newscode": "PBE",
        "newstime": "09:28:03"
      }
    }
  }]
}

var value = a.newsalert[0].newslist[1].headline;
console.log(value);
ellipsis
  • 12,049
  • 2
  • 17
  • 33