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?
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
type ugly.json | python -mjson.tool > pretty.json
–
Guerdon python -mjson.tool input.json > formatted.json
. –
Schock type ugly.json | python -m json.tool > pretty.json
(powershell) –
Murdock python -m json.tool input.json > formatted.json
Otherwise there will be No module named json. __main__
. .tool
is ignored. –
Crocked 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.
get-clipboard | jq "." | set-clipboard
in PowerShell –
Birdcage cat ugly.json | python -m json.tool > pretty.json
worked for me –
Idocrase 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):
- 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:
- Format stdin to stdout:
echo { "x": 1 } | jsonpps
- Format stdin to file
echo { "x": 1 } | jsonpps -o output.json
- Format file to file:
jsonpps input.json -o output.json
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.
You can use Notepad++ (https://notepad-plus-plus.org/downloads/) for formatting large JSON files (tested in Windows).
- Install Notepad++
- Go to Plugins -> Plugins Admin -> Install the 'Json Viewer' plugin. The plugin source code is present in https://github.com/kapilratnani/JSON-Viewer
- After plugin installation, go to Plugins -> JSON Viewer -> Format JSON.
This will format your JSON file
I was also looking for a similar task and I just used Postman beautify to format a 3 MB JSON file and it worked.
© 2022 - 2024 — McMap. All rights reserved.