0

I have a json array parsed from an api as such:

enter image description here

An I want to parse the pageid.

I could do console.log(parsed_json["query"]["pages"]["42743"]["pageid"]) but everytime that "42743" under pages changes.

How could I parse the name under pages so I could use:

console.log(parsed_json["query"]["pages"][" >> ID << "]["pageid"])
jQuerybeast
  • 14,130
  • 38
  • 118
  • 196

1 Answers1

2

Assuming the pages object will only ever have that one, enumerable key, you could do;

var pages = parsed_json.query.pages;
var page;

for (var x in pages) {
    if (pages.hasOwnProperty(x)) {
        page = x;
    }
}

// use pages[page].pageid;

... this enumerates the properties, and records the last one that was enumerated. break on the first or whatever if you need. For laughs, this could be cleaner in ES5:

var pages = parsed_json.query.pages;
var page = Object.keys(pages)[0];

// use pages[page].pageid;
Matt
  • 74,352
  • 26
  • 153
  • 180