Pretty-print JSON with jq, Python and JavaScript

Pretty-print JSON with jq . data.json in a terminal, python3 -m json.tool data.json with Python, or JSON.stringify(value, null, 2) in JavaScript. All three re-indent valid JSON without changing the data. This page has copy-ready commands, the safe way to write a new file, and how each handles JSON Lines and integers bigger than JavaScript can hold.

Last updated

Format a file with jq

jq . data.json
jq --indent 4 . data.json > pretty.json

The dot filter returns the input value. jq prints it with indentation; the second command uses four spaces and saves the output in a new file. Keep input and output filenames different. A shell opens the redirected output before jq reads, so writing back to data.json in the same command can erase the input.

With this input, {"user":{"id":42,"name":"Ada"}}, the default output is:

{
  "user": {
    "id": 42,
    "name": "Ada"
  }
}

Use jq -c . data.json for compact output. That is the reverse formatting direction; the JSON Minifier provides it in the browser.

Use Python's standard library

python3 -m json.tool data.json
python3 -m json.tool --indent 2 data.json pretty.json

Python's JSON command reports invalid input and exits unsuccessfully. In a Python program, use the same parser and choose an indentation width when serializing:

import json
from pathlib import Path

value = json.loads(Path("data.json").read_text(encoding="utf-8"))
Path("pretty.json").write_text(
    json.dumps(value, indent=2, ensure_ascii=False) + "\n",
    encoding="utf-8",
)

Use JSON.stringify in JavaScript

const value = JSON.parse('{"user":{"id":42,"name":"Ada"}}');
const formatted = JSON.stringify(value, null, 2);
console.log(formatted);

The third argument sets the indentation. Use 4 for four spaces or "\t" for tabs. For a file in Node.js:

import { readFileSync, writeFileSync } from "node:fs";

const value = JSON.parse(readFileSync("data.json", "utf8"));
writeFileSync("pretty.json", JSON.stringify(value, null, 2) + "\n");

Handle JSON Lines as a sequence

A .jsonl or NDJSON log contains separate documents on separate lines. It is not one JSON array. jq accepts that sequence directly:

jq . events.jsonl

Indenting it produces multi-line documents, so the output is no longer JSON Lines. Use jq -c . events.jsonl if a log consumer needs one document per line. For large files, choose a streaming parser and processing strategy; a normal JSON.parse call holds the parsed document in memory.

Check precision before calling a round trip lossless

Whitespace between JSON tokens has no meaning, but parse-and-serialize tools can also normalize numbers, escape sequences and duplicate object keys. JavaScript's JSON.parse stores numbers as floating-point values. An integer identifier above Number.MAX_SAFE_INTEGER can change before formatting begins. Keep such IDs in strings or use a parser with an appropriate number representation.

If parsing fails, fix the first syntax error before formatting. The JSON error guide explains common causes, including trailing commas and HTML pasted in place of JSON.

Do it now, locally

Pretty-print, beautify, minify and validate JSON in your browser, 100% client-side. Nothing is uploaded.

Open the JSON Formatter

Frequently asked questions

How do I pretty-print a JSON file with jq?

Run jq . data.json to print an indented document to standard output. To save the result, redirect it to a different file: jq . data.json > pretty.json. Never use the input file as the output target of shell redirection.

Can I format JSON without installing jq?

If Python is installed, run python3 -m json.tool data.json. In JavaScript, serialize a parsed value with JSON.stringify(value, null, 2). Both options run on the machine executing the command.

Why does a JSON Lines file fail with JSON.parse?

JSON.parse expects one document. A JSON Lines file contains one document per line. Parse each non-empty line separately, or use jq, which accepts a sequence of JSON values.

Does JSON.stringify preserve large integer IDs?

JavaScript numbers cannot represent every integer beyond Number.MAX_SAFE_INTEGER (9007199254740991). Precision may already be lost when JSON.parse reads the value. Store identifiers as strings when exact digits matter.