How do I request and process JSON with python?

PythonJsonHttprequest

Python Problem Overview


I am trying to send a GET request to a URL that I know returns data in the form of JSON using python.

I would like to know how to send this request to http://someurl/path/to/json, and how to parse it - preferably to a python dict.

Python Solutions


Solution 1 - Python

For anything with requests to URLs you might want to check out requests. For JSON in particular:

>>> import requests
>>> r = requests.get('https://github.com/timeline.json')
>>> r.json()
[{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...

Solution 2 - Python

Python's standard library has json and urllib2 modules.

import json
import urllib2

data = json.load(urllib2.urlopen('http://someurl/path/to/json'))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsa125View Question on Stackoverflow
Solution 1 - PythonwebjunkieView Answer on Stackoverflow
Solution 2 - PythonSilentGhostView Answer on Stackoverflow