Hamza
Hamza

Reputation: 21

retrive data from a complicated json file

I'm using python to retrive data from an api and what I get is a very complicated json file for me. I want to get latitude and longitude of all points this is the json file.I want to know how to do it since I'm new to json the only thing I did is to to get the""travelTimeInSeconds""

`{
  "formatVersion": "0.0.12",
  "routes": [
    {
      "summary": {
        "lengthInMeters": 1113,
        "travelTimeInSeconds": 801,
        "arrivalTime": "2019-09-15T16:48:19+02:00"
      },
      "legs": [
        {
          "summary": {
            "lengthInMeters": 1113,
            "travelTimeInSeconds": 801,
            "arrivalTime": "2019-09-15T16:48:19+02:00"
          },
          "points": [
            {
              "latitude": 52.50931,
              "longitude": 13.42937
            },
            {
              "latitude": 52.50904,
              "longitude": 13.42912
            },

            {
              "latitude": 52.50274,
              "longitude": 13.43872
            }
          ]
        }
      ],
      "sections": [
        {
          "startPointIndex": 0,
          "travelMode": "pedestrian"
        }
      ]
    }
  ]
}`

Upvotes: 1

Views: 45

Answers (1)

John Gordon
John Gordon

Reputation: 33351

Assuming you've properly converted the json into a python dict with json.loads(), you can use:

# iterate over each route
for route in jsondata['routes']:
    # iterate over each leg in the route
    for leg in route['legs']:
        # iterate over each point in the leg
        for point in leg['points']:
            print (point['latitude'], point['longitude'])

Upvotes: 2

Related Questions