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,301
  • 3
  • 17
  • 27

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
  • 581
  • 5
  • 15
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
  • 11,092
  • 6
  • 67
  • 93
0

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
  • 203,447
  • 57
  • 330
  • 376
0

you can use this code

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