Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I request and process JSON with python?

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.

like image 731
sa125 Avatar asked May 12 '10 09:05

sa125


People also ask

How do you process a JSON response in Python?

Just execute response. json() , and that's it. response. json() returns a JSON response in Python dictionary format so we can access JSON using key-value pairs.

How do you create a JSON request in Python?

To post a JSON to the server using Python Requests Library, call the requests. post() method and pass the target URL as the first parameter and the JSON data with the json= parameter. The json= parameter takes a dictionary and automatically converts it to a JSON string.

How get JSON data from GET request in Python?

To request JSON from a URL using Python, you need to send an HTTP GET request to the server and provide the Accept: application/json request header with your request. The Accept header tells the server that our Python client is expecting JSON.


1 Answers

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/... 
like image 151
webjunkie Avatar answered Sep 29 '22 14:09

webjunkie