← All articles

JSON Pretty Print vs Format: Same Output, Different Workflows

Pretty print, format, and beautify mean the same JSON operation. Learn the practical differences around validation, repair, minify, key sorting, tree view, CLI tools, numeric precision, and safe formatting.

For JSON, pretty print, format, and beautify usually mean the same thing: parse valid JSON, then write it back with indentation and line breaks so a human can read it.

The useful distinction is not "pretty print vs format." The useful distinction is what happens around formatting: validation, repair, minification, key sorting, tree viewing, file rewriting, and whether the parser might change edge-case data such as duplicate keys or very large numbers.

If you only remember one rule, make it this: a safe JSON formatter parses first and serializes second. It does not insert line breaks with regex.

Quick Answer

Term In JSON tools, it usually means Output
Pretty print JSON Add indentation and line breaks Readable JSON text
Format JSON Same as pretty print Readable JSON text
Beautify JSON Same as pretty print Readable JSON text
Minify JSON Remove insignificant whitespace Compact JSON text
Validate JSON Check whether it parses Pass or error
Repair JSON Fix invalid JSON before parsing Changed, valid JSON
View JSON Render a collapsible tree Interactive structure

So yes: if a JSON tool has buttons named Pretty Print, Format, and Beautify, they should produce the same kind of output. A good tool may still offer separate buttons for repair, minify, validate, sort keys, or tree view because those are different operations.

What Pretty Printing Actually Does

Pretty printing takes compact JSON like this:

{"user":{"id":"u_123","name":"Ada","roles":["admin","editor"],"active":true}}

and writes it with consistent whitespace:

{
  "user": {
    "id": "u_123",
    "name": "Ada",
    "roles": [
      "admin",
      "editor"
    ],
    "active": true
  }
}

Those two strings should parse to the same JSON value. The formatter changed whitespace between tokens; it did not change keys, strings, numbers, booleans, nulls, object structure, or array order.

The phrase "between tokens" matters. A newline between object members is formatting. A newline inside a JSON string value is data and must be escaped as \n.

{
  "message": "line one\nline two"
}

A formatter should not turn that escaped newline into a raw line break inside the string. Raw control characters inside strings are invalid JSON, which is a parsing problem, not a formatting style.

Why the Same Operation Has Three Names

The naming mostly comes from different tool cultures:

  • Pretty print is common in programming language documentation and CLI tools.
  • Format is common in editors, IDEs, and code style tools.
  • Beautify came from web-era beautifier tools that made minified code readable.

For JSON, the output is the same category of result: clean, indented text. Use the word your tool, team, or search query uses. The technical question is whether the tool preserves the parsed JSON value.

Formatting Requires Valid JSON

A strict formatter cannot format invalid JSON because it has no parsed structure to indent.

This is valid and can be formatted:

{"name":"Ada","active":true}

These are not valid JSON:

{name:"Ada"}
{'name':'Ada'}
{"name":"Ada",}
{
  // comment
  "name": "Ada"
}

Those inputs need repair or JSONC handling first. If a site says it can "format broken JSON," it is doing two steps:

  1. Repair or normalize the input.
  2. Format the repaired JSON.

That can be useful, but it is not a pure formatting operation. The output may change more than whitespace.

Pretty Print vs Minify

Pretty printing and minifying are opposites at the whitespace level.

Pretty print for humans:

{
  "id": "evt_123",
  "type": "invoice.paid",
  "live": true
}

Minify for transport, logs, or size:

{"id":"evt_123","type":"invoice.paid","live":true}

Both are valid JSON. Both should parse to the same value. Pretty printing helps code review and debugging. Minifying helps when every byte matters or when a system expects one-line JSON.

For the opposite workflow, see How to Minify JSON.

Pretty Print vs Validate

Validation answers "is this JSON syntactically valid?" Formatting answers "can I make this valid JSON easier to read?"

Most formatters validate as a side effect because parsing is the first step. On the command line, jq . file.json is a good example: valid JSON is printed nicely, invalid JSON exits non-zero with a parse error.

jq . response.json
python3 -m json.tool response.json

That makes a formatter useful in scripts:

jq . response.json > formatted.json

If response.json is invalid, the command fails instead of writing a misleading "formatted" file. For a deeper validation workflow, see How to Validate JSON.

Pretty Print vs Repair

Repair changes invalid text into valid JSON. Formatting changes valid JSON into readable JSON.

Use repair when the input has common mistakes:

  • single quotes
  • trailing commas
  • unquoted keys
  • comments
  • Python literals such as True, False, or None
  • markdown code fences around JSON
  • truncated objects or arrays

Use formatting after repair, not before it. The right mental model is:

broken text -> repair -> valid JSON -> format -> readable JSON

For one-off pasted data, JSON Fix can repair and format locally in the browser. For code or API contracts, fix the producer so it emits strict JSON in the first place.

Pretty Print vs Tree View

A formatter gives you text you can copy, commit, or paste into a ticket. A viewer gives you an interface for navigating the same data.

Use formatted text when you need:

  • a Git diff
  • a code sample
  • a README snippet
  • a formatted file on disk
  • a before/after comparison

Use a tree view when you need:

  • collapsible objects and arrays
  • a quick map of a huge response
  • key search without scrolling through thousands of lines
  • a safer way to inspect deeply nested API payloads

For the viewer distinction, see JSON Viewer vs JSON Formatter or open the JSON Viewer.

How to Pretty Print JSON in JavaScript

If you already have a JavaScript value, use JSON.stringify with the third argument:

const value = {
  user: {
    id: 'u_123',
    name: 'Ada',
    roles: ['admin', 'editor']
  }
};

const formatted = JSON.stringify(value, null, 2);
console.log(formatted);

If you start with JSON text, parse it first:

const raw = '{"name":"Ada","active":true}';
const formatted = JSON.stringify(JSON.parse(raw), null, 2);

The third argument can be a number of spaces or a string:

JSON.stringify(value, null, 2);
JSON.stringify(value, null, 4);
JSON.stringify(value, null, '\t');

JavaScript caps indentation at 10 characters. Passing 100 does not create 100-space indentation.

For files, write a trailing newline so Git diffs stay tidy:

import { readFile, writeFile } from 'node:fs/promises';

const raw = await readFile('input.json', 'utf8');
const value = JSON.parse(raw);
const output = `${JSON.stringify(value, null, 2)}\n`;

await writeFile('input.json', output);

If the file matters, write to a temporary path first and replace the original only after parsing and validation succeed.

How to Pretty Print JSON in Python

Python uses json.dumps for values and json.tool for files.

import json

raw = '{"name":"Ada","active":true}'
data = json.loads(raw)

print(json.dumps(data, indent=2))

For a file:

python3 -m json.tool input.json
python3 -m json.tool input.json output.json

If you want stable key order in Python, make it explicit:

json.dumps(data, indent=2, sort_keys=True)

Sorting keys can be helpful for generated fixtures and snapshots. It can be annoying for hand-written configuration where humans group related keys deliberately.

Key Order, Sorting, and Stable Diffs

Plain formatting should preserve object member order as the parser exposes it. Sorting keys is a separate option.

Use sorting when:

  • generated JSON should produce stable diffs
  • snapshot tests change too often
  • object key order is accidental noise
  • two files should be compared structurally

Avoid sorting when:

  • the file is hand-maintained
  • key grouping carries meaning for readers
  • you need to preserve the producer's original order for review
  • arrays are involved and someone is tempted to sort them too

Never sort arrays as part of formatting unless your schema says the array is an unordered set. JSON arrays are ordered data.

Data Changes to Watch For

Most normal JSON values survive parse-and-format unchanged. The risky cases are the same ones that matter in any JSON parser/serializer workflow.

Edge case What can happen Safer approach
Duplicate object keys Many parsers keep only the last value. Treat duplicates as invalid before formatting important data.
Very large integers JavaScript may lose precision beyond safe integer range. Keep IDs and high-precision numbers as strings, or use a parser that preserves big numbers.
NaN or Infinity They are not valid JSON values. Fix the producer or encode them as strings/null by policy.
undefined in JavaScript values JSON.stringify omits object properties and turns array slots into null. Do not format JavaScript objects as if they were already JSON. Validate the intended schema.
Date objects in JavaScript values They serialize to ISO strings. Be explicit about date serialization before formatting.
Escaped characters Output may use different but equivalent escaping. Compare parsed values, not raw text, if escaping style changes.

This is why "formatting does not change data" is true for ordinary valid JSON text, but too casual for production pipelines. The parser and serializer are still part of the workflow.

Line Endings, Final Newlines, and Files

When a formatter rewrites a file, it may also normalize file-level style:

  • indentation width
  • trailing newline
  • line endings (LF vs CRLF)
  • key sorting if enabled
  • spaces after colons and commas

Those are not JSON data changes, but they affect Git diffs. If a pull request changes every line of a file, check whether the formatter changed line endings or indentation. For team repos, pick one formatter and run it consistently instead of mixing editor defaults.

A Practical Formatting Checklist

Use this when the JSON matters:

  1. Confirm the input is strict JSON, JSONC, or broken JSON.
  2. Repair only if the input is a human paste, LLM output, or temporary sample.
  3. Parse with a tool that fails loudly.
  4. Format with the project's indentation style.
  5. Decide whether key sorting is wanted before applying it.
  6. Check for duplicate keys or precision-sensitive numbers if the data is important.
  7. Diff the before and after if the file is going into source control.
  8. Avoid uploading sensitive JSON to server-side tools.

For private payloads, use browser-local tools or local commands. A JSON formatter can see tokens, customer records, webhook bodies, environment dumps, and API responses. Treat pasted data as sensitive until proven otherwise. See Sensitive JSON and local tools for the longer version.

Pretty Print JSON Online

To pretty print quickly, paste JSON into JSON Fix. It formats with consistent indentation and can repair common pasted mistakes first, all in the browser. If the document is large and you need to explore it rather than copy formatted text, use the JSON Viewer.

For repeatable workflows:

Frequently Asked Questions

Is pretty print JSON the same as format JSON?

Yes. In JSON tools, pretty print, format, and beautify usually mean the same operation: parse JSON and write it back with indentation and line breaks.

Does pretty printing change JSON data?

For ordinary valid JSON text, it should not. Whitespace between tokens is insignificant, so compact and formatted JSON parse to the same value. Edge cases such as duplicate keys or unsafe large numbers should be checked before important rewrites.

Can a formatter fix invalid JSON?

A strict formatter cannot. Invalid JSON must be repaired or parsed as JSONC first. Some tools combine repair and formatting, but that is no longer a pure formatting operation.

How do I pretty print JSON in JavaScript?

Use JSON.stringify(value, null, 2) for a JavaScript value. If you start with text, use JSON.stringify(JSON.parse(raw), null, 2) so invalid JSON fails before formatting.

What is the opposite of pretty printing JSON?

Minifying. It removes insignificant whitespace and produces compact JSON, usually for transport, storage, logs, or one-line command output.

Does formatting JSON change key order?

Plain formatting should preserve the order exposed by the parser. Sorting keys is a separate option. Use sorting for stable generated diffs, but avoid it when human grouping matters.

Should I use 2 spaces, 4 spaces, or tabs?

Use the style your project already uses. Two spaces are common in JavaScript projects, four spaces are common in Python examples, and tabs are valid indentation output. The parsed JSON value is the same.

Is it safe to paste JSON into an online formatter?

Only if the tool processes the text locally in your browser or the data is already safe to share. For tokens, customer records, webhook bodies, logs, or internal API responses, prefer local commands or a browser-local tool.

Related Tools & Guides

Sources

Last reviewed July 2026.