Lee
Lee

Reputation: 1465

selecting a specific item in JSON array

I have a JSON object that when I do this:

console.log(response.json);

I get this

{ results:
   [ { address_components: [Object],
       formatted_address: 'Google Bldg 42, 1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA',
       geometry: [Object],
       place_id: 'ChIJPzxqWQK6j4AR3OFRJ6LMaKo',
       types: [Object] } ],
  status: 'OK' }

I want to be able to select formated_address as an example. I've tried variations of console.log(response.json.formatted_address); that but I can't quite figure it out.

Upvotes: 0

Views: 8158

Answers (4)

Omid Gharib
Omid Gharib

Reputation: 321

you can use this code

console.log(response.json.results[0].formatted_address);

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222722

Just access the first element , i.e index 0 of the array and then formatted_address

console.log(response.json.result[0].formatted_address);

Upvotes: 1

Matthias
Matthias

Reputation: 707

You have an object inside an array, so you need to specify the first item in the array.

response.json.results[0].formatted_address

should work.

Upvotes: 1

Black Mamba
Black Mamba

Reputation: 15615

The value is not accessible directly so you need to do something like this.Your formatted_address lies in an array which is present in the result key.So to get your result do something like this

console.log(response.json.result[0].formatted_address);

Upvotes: 0

Related Questions