Search code examples
javascriptarraysjsonstringify

Pull specific data from JSON response


I have a JSON response which I convert to string in javascript by doing

var myObject = JSON.stringify(data);

This prints out the results fine but I need to be able to pull specific data like myObject.route.locationSequence[1] or myObject.route.distance. However neither of those seem to work. Below is my JSON response.

{
    "route": {
        "hasTollRoad": false,
        "computedWaypoints": [],
        "fuelUsed": 5.67,
        "hasUnpaved": false,
        "hasHighway": true,
        "realTime": -1,
        "boundingBox": {
        "ul": {
        "lng": -77.863792,
        "lat": 40.811218
        },
        "lr": {
        "lng": -76.30574,
        "lat": 39.962482
        }
        },
        "distance": 142.909,
        "time": 9872,
        "locationSequence": [
        0,
        2,
        1,
        3
        ]
    }
}

Solution

  • If that is your JSON response then just dont do

    JSON.stringify(data)
    

    Simply use data.route.locationSequence[1]

    Working Fiddle