jQuery and JSON: getting an element by name

Chetan picture Chetan · Feb 10, 2010 · Viewed 32.4k times · Source

I have the following JSON:

var json = { "system" : { "world" : { "actions" : { "hello" : { "src" : "hello world/hello world.js", "command" : "helloWorld" } } } } }

I have the following javascript:

var x = "system";
// get the contents of system by doing something like json.getElementByName(x)

How do I get the contents of system using json and x in jQuery?

Answer

Doug Neiner picture Doug Neiner · Feb 10, 2010

Just use:

var x = "system";
json[x];

It is a key/value system of retrieval, and doesn't need a function call to use it.