0

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.

Lee
  • 1,389
  • 3
  • 18
  • 28

4 Answers4

1

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.

Matthias
  • 648
  • 6
  • 18
1

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

console.log(response.json.result[0].formatted_address);
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

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);
Black Mamba
  • 13,632
  • 6
  • 82
  • 105
0

you can use this code

console.log(response.json.results[0].formatted_address);
Omid Gharib
  • 301
  • 1
  • 9
  • 26