I am trying to store the response from API in JSON format. I got the JSON response in a string format and stored in a file. How do I make it or convert with indent as we see in the onlineJSONViewer application? or in JSON format.
Code I used to store in a file.
def test_url(self):
resp =requests.get(www.myurl.com)
data = resp.text
f = open("19octfile.json", "w")
f.write(data)
f.close()
This Code stores the response in 19octfile.json in below format:
{"data": [{"id":"myname","id":"123","name":"myname","user":"m3","provider":"user","region":"india"}]}
Now, How can I store the response with indent i.e in JSON format so that user can understand easily when reads.
My different TRY but in vain:
with codecs.open('data.json', 'w', 'utf8') as f:
f.write(json.dumps(data, sort_keys=True, ensure_ascii=False))
This code give the same result with unicode character no indent
with open('17octenv71232111.json', 'w') as outfile:
json.dump(data,outfile)
outfile.close()
This code also same result with unicode char and no indent
Can any one help me is there any library that can do the format work or any code to help.