Domanda

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?

È stato utile?

Soluzione

With python >= 2.6 you can do the following:

For Mac/Linux users:

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

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

type ugly.json | python -mjson.tool > pretty.json

Altri suggerimenti

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.

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

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top