1

I am very new to JSON, have been pestered to get this done.I have to get the value of "extract" as in the given diagram. This diagram is a object diagram of a json. The value 21721040 can be random, thus I do not know the name of the object whose one of the members I seek.

Thus I cannot do something like

query.pages.21721040.extract

enter image description here

So how can I get the value of "extract"?

Out of curiosity should the object whose name is a numeral create an error when I try to access one of its members? Or they just work? For example in this case one of the object's name is "-1".

enter image description here

If I try to access the value "url" after parsing in JS like this:

query.pages.-1.imageinfo.0.url

Will it throw an error?

5
  • Do you know the id beforehand? Commented Jul 1, 2014 at 4:32
  • try query.pages["-1"].imageinfo[0].url Commented Jul 1, 2014 at 4:33
  • Nope. Can only be known at runtime. Commented Jul 1, 2014 at 4:33
  • Is this data coming from Wikipedia API by any chance? Commented Jul 1, 2014 at 4:49
  • @Jack Yes it is being fetched from their API. Commented Jul 1, 2014 at 5:01

3 Answers 3

1

try this :

query.pages[Object.keys(query.pages)[0]].extract
Sign up to request clarification or add additional context in comments.

2 Comments

Javascript's indexes for json are mashed up. Can you comment on that, will your method always return the right value?
It's different than my answer due to Object.keys() returning an actual array and not an object.
1

try query.pages["-1"].imageinfo[0].url. actually all the fields can be considered as string. so you can write query["pages"]["-1"]["imageinfo"]["0"]["url"]

if you do not know the key, use Object.keys() to find out the keys associated to that object. this key can be any key belong to the object or the object it has been inherited from. to find out only the object's own key user hasOwnProperty

Comments

1

If you don't know the property name, you can iterate over the properties on the object yourself:

for (var key in query.pages) {
    if (query.pages.hasOwnProperty(key)) {
        console.log(query.pages[key].extract);
    }
}

Alternatively, you can use the relatively modern Object.keys() to obtain an array of keys that the object has.

7 Comments

Is it a property or a child object? From the very limited knowledge I have it looks like an object as it contains key:value pairs and its not itself a key value pair.
Can it be modified to register only the property containing "extract"? Or I need to check for empty values?
@AshwinSingh Is what a property or child object? I don't think property and child object are mutually exclusive terms.
@AshwinSingh I don't understand what you mean by "register the property containing 'extract'"; could you clarify?
I mean log. I am under the impression that your method will log null values for items not containing any "extract" member. Can the loop be done for a simple straight-forward access as there are cases where there can be a lot of keys..
|

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.