Reading JSON files from curl in Python

python3 use curl to request data
For example, change the curl to python in the below.

$ curl -XPOST http://httpbin.org/post -H "Content-Type:application/json" -d '{"attribute":"value"}'

intstall urllib3 using $ python -m pip install urllib3


import urllib3
import json
data = {'attribute': 'value'}
encoded_data = json.dumps(data).encode('utf-8')
r = http.request(
    'POST',
    'http://httpbin.org/post',
    body=encoded_data,
    headers={'Content-Type': 'application/json'}
)
json.loads(r.data.decode('utf-8'))['json']

print result

{'attribute': 'value'}

ref: https://urllib3.readthedocs.io/en/latest/user-guide.html


In general, if you have a command that prints to its stdout then you could get the output without storing it on the disk using subprocess.check_output:

from subprocess import check_output

output = check_output(['source', 'arg1', 'arg2'])

In your case, you could use urllib2 or requests Python modules instead of curl command as shown in @Esparta Palma's answer.


You can use the stdlib (json & urllib2) to avoid the use of external commands:

import json
import urllib2
url = "http://httpbin.org/get"
response = urllib2.urlopen(url)
data = response.read()
values = json.loads(data)

But I'd recommend to use requests to simplify your code. Here a sample from the docs:

import requests
r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
r.status_code
200
r.headers['content-type']
'application/json; charset=utf8'
r.encoding
'utf-8'
r.text
u'{"type":"User"...'
r.json()
{u'private_gists': 419, u'total_private_repos': 77, ...}

Python 3 Update

Please consider that in Python3 urllib2 does not exists anymore, you should use urllib which comes in the standard library

req = urllib.request.Request(url)
response = urllib.request.urlopen(req)
data = response.read()
values = json.loads(data)    

Tags:

Python

Curl

Json