0

I am getting the below json return from wiki:

{
"query": {
    "pages": {
        "1514": {
            "pageid": 1514,
            "ns": 0,
            "title": "exampletitle",
            "extract": "Example extract"
        }
    }
}
}

How can I access the "extract" value from this json? I will not know the page id (1514) and can't simply do a json.query.pages.1514.extract;

(anyway .1514 will not work)

Thank you!

1
  • if you don't know page number, how do you suppose to get data in case there are 20 pages in pages object? Or you need ALL of them and looking for a way to iterate it as array? Commented Jul 11, 2013 at 13:54

3 Answers 3

2

You can iterate over the pages object to access its members

for(var key in json.query.pages){
    if(json.query.pages.hasOwnProperty(key)){
        console.log(json.query.pages[key].extract);
    }
 }
Sign up to request clarification or add additional context in comments.

Comments

0

json.query.pages['1514'].extract should work

1 Comment

Ah! However, I won't know that it's 1514 unless I can access the pageid
0

use JSON.parse()

ex:

var [your_parsed_JSON] = JSON.parse([your_JSON_variable])


heres a similar stackoverflow question with more detail/examples:

Parse JSON in JavaScript?


EDIT: actually it appears you are trying to access the contents of a container that you do not know. That's like trying to find the "lost treasure" without knowing where it is.

Reorganize your data structure.

1 Comment

It's the wikimedia API though, I don't have control over the data returned!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.