0

How I can access the value of location address from this json Object.

How to get values from string

{
    "total": 1494,
    "businesses": [
        {
            "price": "$$",
            "phone": "+19055222999",
            "name": "Earth To Table : Bread Bar",
            "url": "https://www.yelp.com/biz/earth-to-table-bread-bar-hamilton?adjust_creative=o3c6gGE-jHIf_ycxKdETJA&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=o3c6gGE-jHIf_ycxKdETJA",
            "location": {
                "address1": "Mars",
                "city": "Toronto",
                "address3": "",
                "address2": "",
                "state": "ON",
                "country": "CA"
            }
        }
    ]
}

I have tried `

str = JSON.parse(string.businesses.location[0]);  

But it returns string.businesses.location is not a function

5
  • 3
    str = JSON.parse(string).businesses[0].location; Commented Nov 29, 2016 at 23:10
  • Alexander, you see how businesses is an array accessed by a zero-based index? You can iterate this array with a loop such as for(var i = 0; i < 5; i++) { businesses[i].location }; Commented Nov 29, 2016 at 23:18
  • someArray.slice(0,5) Commented Nov 29, 2016 at 23:19
  • @Pointy you can write your answer and I will mark it correct! Commented Nov 29, 2016 at 23:20
  • @Alexander it's OK somebody got it - I was on the phone and couldn't really type :) Commented Nov 29, 2016 at 23:55

2 Answers 2

2

You have to convert the JSON string to a JSON Object first and then try to access the data.

var jsonObj = JSON.parse(jsonString);
var location = jsonObj.businesses[0].location;
Sign up to request clarification or add additional context in comments.

Comments

-1

In the JSON, the location doesn't have '[' so is not a list, but the businesses have the '[',so you should change:

str = JSON.parse(string.businesses.location[0]);  

for this

//first parse all the json
var dataJson = JSON.parse(string);
//second take te value
var address = dataJson.businesses[0].location.address1

Check the example here!!!

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.