How to see a large JSON file pretty printed on Ubuntu? [closed]
Asked Answered
R

7

59

I would like to hear your suggestions on how to handle a large (40MB) JSON file on Ubuntu. I would like to see it pretty printed in vim or gedit or any other editor. One can find numerious tutorials on how to prettify the JSON, however, they do not have to deal with large input. I also imagine I could pipe the data through pygments or any other syntax highlighter. I am curious to hearing your ideas.

Example download:

wget -O large-dataset.json http://data.wien.gv.at/daten/wfs?service=WFS&request=GetFeature&version=1.1.0&typeName=ogdwien:BAUMOGD&srsName=EPSG:4326&outputFormat=json

Please mind the download size!


EDIT: I found out that meld is working somewhat. The application does not load the whole file at once which would block the user interface. Instead it sequentially reads the file content.

Rimrock answered 22/5, 2012 at 14:32 Comment(3)
dadroit.comImpassion
Try munis-pretty-json npm module to visualize json log data on console with 'less' command.Karolkarola
@FallenApart best json viewer since this time :)Kolk
S
80

Python JSON's module can do this too (python -m json.tool), e.g.:

cat myjsonfile.json | python -m json.tool > pretty.json
Soniasonic answered 19/9, 2017 at 13:12 Comment(2)
Even faster than Sublime! Thanks Also, in order you just want to preview the file, you can add ` | less` instead of ` > pretty.json`.Kalila
Works perfectlyIl
J
34

If you just want to visualize (and search) a json file, Firefox does a pretty good job. I don't have a 40MB file on hand, but it easily handled a 9MB one.

Just drag the JSON file to Firefox, or run:

firefox your_file.json

Jedthus answered 12/2, 2019 at 17:7 Comment(2)
Remember to keep the file extension as .jsonReisman
Doesn't work for large files. I have ~60Mb file and Firefox shows it as a text file.Haploid
D
29

jq is a lightweight commandline JSON processor and works well! for this you need to install jq with the command below(if you are using apt package manager).

sudo apt-get install jq

Below command will pretty json to a new file.

 jq '.' non-pretty.json > pretty.json

Also we can filter the json with jq, I found it to be very helpful while working with large geojson files, for instance below command will save only properties of first feature.

jq '.features[0].properties' geojson_file.json > pretty.json

Hope this will be helpful!

Delhi answered 25/10, 2019 at 8:40 Comment(4)
I had UTF-8 problem with solution python -m json.tool. Firefox could not open big file. But jq helped me finally.Ocam
searched for a tool like this for half an hour, and it's the first one that can process my 230 MB json file, Thank youRunoff
jq is more robust on near-json formatted logs. python json.tool may be a bit too strict.Morganite
This jq filter is awesome. Thanks for the tip!Brookbrooke
M
5

Do you have KDE or any other visual environment? If yes, have you tried using the chrome extension JSONView?

Motmot answered 22/5, 2012 at 14:36 Comment(3)
I am using Gnome on Ubuntu 10.10. I use JSON formatter and tried your suggestion. Both work great when I load a JSON file via HTTP. However, no syntax highlighting is applied when I simple open the file with Chromium. Strange also, that the other applications almost die when they open the large file while Chromium has no problem in the same case.Rimrock
No. I grand +1 since JSONView seems to faster than JSON formatter. However, they do not solve my problem as I stated already.Rimrock
I found out how to format local files also. You have to enable access to local files for the extension explicitly.Rimrock
C
4

I usually use Sublime Text for this purpose. There is a dedicated plugin for this job.

The plugin Pretty JSON parses the JSON contents selected, and prints them in a structured way.

All you need to do is to select the contents and press Ctrl+Alt+j.

Cromer answered 26/12, 2018 at 11:58 Comment(0)
T
2

The core usage is pretty formatting large json. I tested Chrome extension JSON View with 25MB json file. It crashes on loading this as a local file or from network. By crash, I mean JSON will not get formatted and on looking into JSON view options, you will get a crash message. I also tried similar addons for firefox. I tried online json formatters as well.

Found this library - jsonpps. Works pretty well to pretty format large json from command line, taking input and saving the formatted json as separate file. It can also save in the same file (need optional parameter)

One drawback, To install and run, one should be familier with Java and Maven.

To install & run:

git clone https://github.com/bazaarvoice/jsonpps.git
mvn clean package
cd target 
java -jar jsonpps-1.2-SNAPSHOT.jar -o /path/to/output.json /path/to/largeInput.json

This solution is not restricted to Ubuntu. It should work on any operating system.

Thermal answered 15/12, 2014 at 10:31 Comment(0)
L
2

Use the external tool option. This worked for me http://www.milosev.com/downloads/websphere/117-linux/ubuntu/454-json-prettifier-for-gedit.html

Levinson answered 8/8, 2018 at 9:0 Comment(1)
This does not workDedra

© 2022 - 2024 — McMap. All rights reserved.