0

I'm trying to access a nested JSON object from the Wikipedia API where the object name is the primary key.

Example:

{
    "pages": {
        "13595": {
        "pageid": 13595,
        "ns": 0,
        "title": "London Heathrow Airport",
        "extract": "London Heathrow Airport (IATA: LHR, ICAO: EGLL) is a major international airport in West London, England, United Kingdom."
        }
    }
}

13595 is the pageid and is always different for each page. I want to get the value of "extract".

Any Idea how? I'm using jQuery.

svick
  • 236,525
  • 50
  • 385
  • 514
Omar.Ebrahim
  • 862
  • 1
  • 10
  • 30

2 Answers2

1

If you're sure that there'll be only one page, the following instruction returns "13595" in your example:

var json = { ... }
Object.keys(json["pages"])[0]
lucach
  • 43
  • 1
  • 6
1

I should have looked at How to access nested object in JSON returned by Wikipedia API

Solved by:

for (var id in pages){
    var extract = pages[id].extract;
    if (extract){
        $('#txt_airport_extract').text(extract);
    }
}
Community
  • 1
  • 1
Omar.Ebrahim
  • 862
  • 1
  • 10
  • 30