Haradzieniec
Haradzieniec

Reputation: 9338

Get the JSON value by a specific key

I'm almost finished with extracting the value from the JSON string. When I do console.log(rs.query.pages[19].revisions[0]) I get

Object {key1: "value1", key2: "value2", *: "value3"}

But, all I need is to extract value3 only. How to get that value by the key *?

When I try (and of course it won't work) console.log(rs.query.pages[19].revisions[0].*) I get

Uncaught SyntaxError: Unexpected token *

What's the correct way to get value3?

Upvotes: 0

Views: 1402

Answers (2)

sirbombay
sirbombay

Reputation: 419

console.log(rs.query.pages[19].revisions[0]["*"])

Upvotes: 0

Pointy
Pointy

Reputation: 414086

You can put "*" as a string in square brackets:

console.log(rs.query.pages[19].revisions[0]["*"])

That'll always work, regardless of how "messy" the property name is. When property names look like valid JavaScript identifiers, you can use the . notation.

Upvotes: 1

Related Questions