Best way to format large JSON file? (~30 mb)
Asked Answered
E

6

75

I need to format a large JSON file for readability, but every resource I've found (mostly online) doesn't deal with data say, above 1-2 MB. I need to format about 30 MB. Is there any way to do this, or any way to code something to do this?

Endure answered 9/11, 2013 at 11:8 Comment(1)
Do you simply need to read it? Or you want to format the output before sending it do clients?Mckean
F
154

With python >= 2.6 you can do the following:

For Mac/Linux users:

cat ugly.json | python -m json.tool > pretty.json

For Windows users (thanks to the comment from dnk.nitro):

type ugly.json | python -m json.tool > pretty.json
Funderburk answered 9/11, 2013 at 11:17 Comment(7)
For windows users: type ugly.json | python -mjson.tool > pretty.jsonGuerdon
Using a recent Python3 version you can simplify the command python -mjson.tool input.json > formatted.json.Schock
It looks like Python limits the size of the file to 2Gb. So if you have really large files, this solution won't work for you.Ipsambul
I'm unsure why all the windows pythons commands don't have a space between -m and json. Only this worked for me: type ugly.json | python -m json.tool > pretty.json (powershell)Murdock
I just used it on a .json file that contained Japanese text and it's converting the Japanese text to "\u304a\u3061\u3093\u3061\u3093" strings. It formats the .json correctly but seems to also transform the data so be careful.Spirit
It crashes, by lack of RAMViper
There must be a space between -m and json.tool (windows powershell). python -m json.tool input.json > formatted.json Otherwise there will be No module named json. __main__. .tool is ignored.Crocked
S
55

jq can format or beautify a ~100MB JSON file in a few seconds:

jq '.' myLargeUnformattedFile.json > myLargeBeautifiedFile.json

The command above will beautify a single-line ~120MB file in ~10 seconds, and jq gives you a lot of json manipulation capabilities beyond simple formatting, see their tutorials.

Supramolecular answered 29/1, 2019 at 11:13 Comment(8)
It loads everything into RAMAlesha
Worked fine on a 100MB file, but not on a 900MB file. :(Toein
In windows, I had to use "." instead of '.'Illjudged
This worked for me on a 172MB file, whereas Visual Studio Code just stared me straight in the face and said "I won't do that, it's too big." when I asked it to "format" the document. Thanks!Plerre
If anyone copy-pastes large JSON and wants to skip having a file, one can run get-clipboard | jq "." | set-clipboard in PowerShellBirdcage
another window example: type big_unformatted.json | c:\path_to_jq\jq-win64.exe > big_formatted.jsonGrandaunt
Good tip, worked great here... using WIndows 11 64 bits! Thanks! You can have it installed with ease if you use Chocolatey.Crackbrained
Didn't work for me for a file ~112 MB. cat ugly.json | python -m json.tool > pretty.json worked for meIdocrase
A
9

jsonpps is the only one worked for me (https://github.com/bazaarvoice/jsonpps).
It doesn't load everything to RAM unlike jq, jsonpp and others that I tried.

Some useful tips regarding installation and usage:

Download url: https://repo1.maven.org/maven2/com/bazaarvoice/jsonpps/jsonpps/1.1/jsonpps-1.1.jar

Shortcut (for Windows):

  1. Create file jsonpps.cmd in the same directory with the following content:
    @echo off java -Xms64m -Xmx64m -jar %~dp0\jsonpps-1.1.jar %*

Shortcut usage examples:

  1. Format stdin to stdout:
    echo { "x": 1 } | jsonpps
  2. Format stdin to file
    echo { "x": 1 } | jsonpps -o output.json
  3. Format file to file:
    jsonpps input.json -o output.json
Alesha answered 18/7, 2020 at 11:26 Comment(0)
H
1

Background-- I was trying to format a huge json file ~89mb on VS Code using the command (Alt+Shift+F) but the usuals, it crashed. I used jq to format my file and store it in another file.

A windows 11 use case is shown below.

step 1- download jq from the official site for your respective OS - https://stedolan.github.io/jq/

step 2- create a folder in the C drive named jq and paste the executable file that you downloaded into the folder. Rename the file as jq (Error1: beware the file is by default an exe file so do not save it as 'jq.exe' save it only as 'jq')

step 3- set your path variable to the URL of the executable file.

step 4- open your directory on cmd where the json file is stored and type the following command - jq . currentfilename.json > targetfilename.json

replace currentfilename with the file name that you want to format replace targetfilename with the final file name that you want your data formatted in


within seconds you should see your target file in the same directory in a formatted version which can now be opened on VS Code or any editor for that matter. Any error related to the recognizability of jq as a command can be traced back with high probability to Error 1.

Hufuf answered 9/10, 2022 at 15:14 Comment(0)
W
1

You can use Notepad++ (https://notepad-plus-plus.org/downloads/) for formatting large JSON files (tested in Windows).

  1. Install Notepad++
  2. Go to Plugins -> Plugins Admin -> Install the 'Json Viewer' plugin. The plugin source code is present in https://github.com/kapilratnani/JSON-Viewer
  3. After plugin installation, go to Plugins -> JSON Viewer -> Format JSON.

This will format your JSON file

Wondrous answered 22/2, 2023 at 9:17 Comment(0)
O
-2

I was also looking for a similar task and I just used Postman beautify to format a 3 MB JSON file and it worked.

Otolith answered 17/1 at 8:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.