refer to an element of JSON (Javascript) object refer to an element of JSON (Javascript) object arrays arrays

refer to an element of JSON (Javascript) object


That's not exactly very good JSON above there, in the case of the Agents value the second key will override the first.

You probably meant:

 "Agents" : [     {"name" : "Bob Barker"},     {"name" : "Mona Mayflower"}  ],

Then you'd access the first agent's name as

homes[0]['Agents'][0]['Name']

Similarly, to get one of the values from the Listings, you'd do something akin to:

homes[0]['Listings'][0]['city']- or -homes[0].Listings[0].city

The dot syntax can be used wherever there is a valid identifier, else you need to use the array syntax.

As a side note, I'm not sure the structure of the data, but it's possible that you can eliminate the outer-level [] that's enclosing your whole structure in an array. Then you wouldn't need to access everything as homes[0]['Listings'] and instead simply homes['Listings'].


Your JSON syntax is wrong. You can't have the same key twice in an object. Instead, you need an array:

var homes = {  "Agents" : [    { "name" : "Bob Barker" },    { "name" : "Mona Mayflower" }  ],  ...}

Then you can access the agents like so:

homes.Agents[1] // => { "name": "Mona Mayflower" }// orhomes.Agents[1].name // => "Mona Mayflower"


Homes is an Array, so your first accessor is index based.

homes[0]

Agents is an Object, and Object containing two keys of the same name. That is a no-no.

If you are defining this data yourself, you should change

"Agents": {    "name" : "Bob Barker",    "name" : "Mona Mayflower"}

to

 "Agents": [        {"name" : "Bob Barker"},        {"name" : "Mona Mayflower"}    ]

Then you could access the data in question by

homes[0].Agents[1].name