3

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.

3
  • 1
    you can pass indent parameter with value as int Commented Oct 19, 2018 at 13:29
  • Sorry , can you please explain in detail i am not getting how to use int Commented Oct 19, 2018 at 13:31
  • 1
    @dow reading the manual would be faster Commented Oct 19, 2018 at 14:27

2 Answers 2

5

The function json.dumps accepts a named parameter indent. From the documentation:

If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0, or negative, will only insert newlines. None (the default) selects the most compact representation.

First you need to load the json file contents into a python object. Your current code is passing the json string to json.dumps. Use the following:

j = json.loads(data)
f.write(json.dumps(j, sort_keys=True, indent=4))

Here the json.loads function turns the json string into a python object which can be passed to json.dumps.

Sign up to request clarification or add additional context in comments.

1 Comment

Great!!! thanks for teaching me! I learnt that we need to first load and then only print for our desired requirements
2
import json
d={"data": [{"id":"myname","id":"123","name":"myname","user":"m3","provider":"user","region":"india"}]}
print(json.dumps(d,indent=2))

To write to file

with open('17octenv71232111.json', 'w') as outfile:
   outfile.write(json.dumps(d,indent=2))

1 Comment

Nope, I tried but in same string paragraph format with \ all over.I need to write in file not print in cmd prompt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.