Download a json from a HTTPResponse in python

Viewed 33

I have the next code ,my url variable is an http.client.HTTPResponse object, and contents a json, i need save this json file and save in my computer , but i can't figure out how

import io
import urllib.request, json 
import pandas as pd

hdr = { 'Authorization' : "ApiKey"}
url="some_url"
req = urllib.request.Request(url, headers=hdr)
url= urllib.request.urlopen(req)
1 Answers

Perhaps json.dump(data, file) is what you're looking for.

url="https://yourlinkhere"
req = urllib.request.Request(url)
res= urllib.request.urlopen(req)

data = json.load(res)

with open('file.json', 'w') as f:
    json.dump(data, f)

Start by storing the json data in a variable data which is a dictionary. Then create a json file, file.json and dump the data into it.

Related