Pretty-Print JSON Data to a File using Python
Asked Answered
O

8

204

A project for class involves parsing Twitter JSON data. I'm getting the data and setting it to the file without much trouble, but it's all in one line. This is fine for the data manipulation I'm trying to do, but the file is ridiculously hard to read and I can't examine it very well, making the code writing for the data manipulation part very difficult.

Does anyone know how to do that from within Python (i.e. not using the command line tool, which I can't get to work)? Here's my code so far:

header, output = client.request(twitterRequest, method="GET", body=None,
                            headers=None, force_auth_header=True)

# now write output to a file
twitterDataFile = open("twitterData.json", "wb")
# magic happens here to make it pretty-printed
twitterDataFile.write(output)
twitterDataFile.close()

Note I appreciate people pointing me to simplejson documentation and such, but as I have stated, I have already looked at that and continue to need assistance. A truly helpful reply will be more detailed and explanatory than the examples found there. Thanks

Also: Trying this in the windows command line:

more twitterData.json | python -mjson.tool > twitterData-pretty.json

results in this:

Invalid control character at: line 1 column 65535 (char 65535)

I'd give you the data I'm using, but it's very large and you've already seen the code I used to make the file.

Oilstone answered 7/2, 2012 at 2:37 Comment(4)
I doubt you actually want to write binary data ("wb")Madrigalist
I was taught this was necessary for Windows machines and thus far has worked for all of my assignments. If you can offer documentation as to why this might be incorrect, I'd be happy to look at it.Oilstone
It's only necessary if you're working with binary files, or other cases where the specific form of line ending (e.g. \r\n vs \n) is important. See #3258369. In your case, you want windows friendly line endings, but you might not get that from the twitter endpoint, so you should open in text mode.Madrigalist
Does this answer your question? How to prettyprint a JSON file?Atonal
Y
176

You should use the optional argument indent.

header, output = client.request(twitterRequest, method="GET", body=None,
                            headers=None, force_auth_header=True)

# now write output to a file
with open("twitterData.json", "w") as twitterDataFile:
    # magic happens here to make it pretty-printed
    twitterDataFile.write(
        simplejson.dumps(simplejson.loads(output), indent=4, sort_keys=True)
    )
Yod answered 7/2, 2012 at 3:14 Comment(4)
Thank you, that worked perfectly. Can you explain why "sort_keys" needs to be in there?Oilstone
It doesn't need to be there but it makes things very pretty and alphabetically sorted. I tend to use it when I want human readable output.Yod
Well explained thank you -however not trying to be a &$&# but open/close to write a file isn't encourage, the with structure is generally preferable: with open("name_of_file.json", "w") as f: f.write(my_formatted_json_var) Advantage being you're sure the file will close, say on bigger snippets...Ladder
with syntax is definitely nicer, but I try to scale my answers to my audienceYod
C
134

You can parse the JSON, then output it again with indents like this:

import json
mydata = json.loads(output)
print json.dumps(mydata, indent=4)

See http://docs.python.org/library/json.html for more info.

Chlorous answered 7/2, 2012 at 2:41 Comment(3)
@Zelbinian: yes it works for simplejson as well.Take a look here simplejson.googlecode.com/svn/tags/simplejson-1.9.1/docs/…Squatter
This results in an empty file. header, output = client.request(twitterRequest, method="GET", body=None, headers=None, force_auth_header=True) twitterDataFile = open("twitterData.json", "wb") json.dumps(json.loads(output), twitterDataFile, indent=4) twitterDataFile.close()Oilstone
@Oilstone - json.dumps returns a string. json.dump writes to a file.Chlorous
S
118
import json

with open("twitterdata.json", "w") as twitter_data_file:
    json.dump(output, twitter_data_file, indent=4, sort_keys=True)

You don't need json.dumps() if you don't want to parse the string later, just simply use json.dump(). It's faster too.

Schnell answered 28/1, 2018 at 12:13 Comment(0)
S
16

You can use json module of python to pretty print.

>>> import json
>>> print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
{
    "4": 5,
    "6": 7
}

So, in your case

>>> print json.dumps(json_output, indent=4)
Squatter answered 7/2, 2012 at 2:42 Comment(5)
Tried that route and that unfortunately doesn't work as well as you'd think.Oilstone
@Zelbinian: Exactky what do you mean by doesn't work as well.?Squatter
It outputted the data in a single line in what looked to be Python dict syntax instead of pretty-printed Json syntaxOilstone
Include the output in you question as an edit.So,we can see it.Squatter
using this, arrays are listed out as many lines of each value, it'd be nice to keep the array on one line.Goad
C
9

If you are generating new *.json or modifying existing josn file the use "indent" parameter for pretty view json format.

import json
responseData = json.loads(output)
with open('twitterData.json','w') as twitterDataFile:    
    json.dump(responseData, twitterDataFile, indent=4)
Concepcionconcept answered 16/9, 2019 at 5:53 Comment(0)
T
4

If you already have existing JSON files which you want to pretty format you could use this:

    with open('twitterdata.json', 'r+') as f:
        data = json.load(f)
        f.seek(0)
        json.dump(data, f, indent=4)
        f.truncate()
Telescopium answered 21/2, 2019 at 14:50 Comment(0)
K
3
import json
def writeToFile(logData, fileName, openOption="w"):
  file = open(fileName, openOption)
  file.write(json.dumps(json.loads(logData), indent=4)) 
  file.close()  
Kalvin answered 2/12, 2019 at 20:38 Comment(1)
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.Stopover
S
-1

You could redirect a file to python and open using the tool and to read it use more.

The sample code will be,

cat filename.json | python -m json.tool | more
Schwann answered 4/6, 2015 at 21:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.