Alexander
Alexander

Reputation: 313

How To access Inter Json Object Object value

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

Upvotes: 0

Views: 167

Answers (2)

Cristina Carrasco
Cristina Carrasco

Reputation: 713

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!!!

Upvotes: -1

Debashish Paul
Debashish Paul

Reputation: 197

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;

Upvotes: 2

Related Questions