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"])
1
  • That's not "parsing", that's "accessing" - you have the parsed object already Commented Mar 3, 2013 at 13:13

1 Answer 1

1

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;
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.