-1

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)
alibustami
  • 198
  • 2
  • 13
Jg9912.
  • 27
  • 4
  • 1
    i think your question was answered here: https://stackoverflow.com/questions/13921910/python-urllib2-receive-json-response-from-url – josh_kean Sep 16 '22 at 22:01
  • They just convert the json into a python dictionary , to be able to read it. I don't want read it :/ , i want save it as a file – Jg9912. Sep 16 '22 at 22:03

1 Answers1

1

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.

Jackie
  • 198
  • 12