0

I am trying to figure out how to get a json output in python. here is the url:

http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&sensor=false

whose output would be like this

{
  "status": "OK",
  "origin_addresses": [ "Vancouver, BC, Canada", "Seattle, État de Washington, États-Unis" ],
  "destination_addresses": [ "San Francisco, Californie, États-Unis", "Victoria, BC, Canada" ],
  "rows": [ {
    "elements": [ {
      "status": "OK",
      "duration": {
        "value": 340110,
        "text": "3 jours 22 heures"
      },
      "distance": {
        "value": 1734542,
        "text": "1 735 km"
      }
    }, {
      "status": "OK",
      "duration": {
        "value": 24487,
        "text": "6 heures 48 minutes"
      },
      "distance": {
        "value": 129324,
        "text": "129 km"
      }
    } ]
  }, {
    "elements": [ {
      "status": "OK",
      "duration": {
        "value": 288834,
        "text": "3 jours 8 heures"
      },
      "distance": {
        "value": 1489604,
        "text": "1 490 km"
      }
    }, {
      "status": "OK",
      "duration": {
        "value": 14388,
        "text": "4 heures 0 minutes"
      },
      "distance": {
        "value": 135822,
        "text": "136 km"
      }
    } ]
  } ]
}

How can i print this output in python ?
Can anyone help me out ?
Thanks

3
  • Not sure what you mean "print this output in Python". Anyway: docs.python.org/release/2.6.7/library/json.html#module-json Commented Jun 29, 2012 at 7:43
  • @brunodesthuilliers agreed the title is misleading - "how to use" and "how to 'print'" are different things Commented Jun 29, 2012 at 8:13
  • @JonClements if I am printing it aint I using it ? Commented Jun 29, 2012 at 9:20

2 Answers 2

2

Depending on your version of Python, importing JSON might not be enough.

If you are running a version of python less than 2.6, you need to install simplejson from your commandline.

pip install simplejson

After that, just import normally.

import simplejson as json

The following should work in Python 2.x. There are a few differences in 3.x, I'll leave that as an exercise for your imagination.

try:
    import json
except:
    import simplejson as json
url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&sensor=false"
contents = urllib2.urlopen(url).read()
json_array = json.loads(contents)
print repr(json_array)
Sign up to request clarification or add additional context in comments.

Comments

0
>>> import json
>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')

http://docs.python.org/library/json.html

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.