0

I'm trying to get the "formatted_address" value from this JSON file. I'm new to this and found the documentation quite confusing. The code I have now is the following where the variable "location" is the url generated like the one above.

$.getJSON(location, function( data ){
  stad = data.results.formatted_address;
  console.log(stad);
 });

How would I achieve this?

2 Answers 2

1

results is an array, so you need to access it as one. Given your example with only one item, you can access it directly by index:

var stad = data.results[0].formatted_address; // = "'s-Hertogenbosch, Netherlands"

If there were multiple items in the array you would need to loop through them:

for (var i = 0; i < data.results.length; i++) {
    var stad = data.results[i].formatted_address;
    // do something with the value for each iteration here...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this worked! Looking back at it it's so obvious but all the documentation made me nuts! I'll check it as an answer in 5 minutes.
0
$.each(data.results,function(key,value){
     console.log(value.formatted_address); //'s-Hertogenbosch, Netherlands
 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.