jsondecode.com logo

JSON Formatter & Beautifier

Format JSON online instantly. Paste raw, minified, or messy JSON and get clean, indented output with syntax validation and error highlighting. Choose 2 spaces, 4 spaces, or tabs. Runs entirely in your browser — your data never leaves your machine.

Input JSON
Indent:
Formatted Output
Formatted JSON will appear here

What Is JSON Formatting?

JSON formatting (also called JSON beautifying or JSON pretty print) is the process of adding indentation, line breaks, and consistent spacing to a JSON document to make it human-readable. Raw API responses, log files, and minified config files are often delivered as a single line of text — impossible to scan visually. A JSON formatter restructures this data with proper nesting so you can instantly see the shape, depth, and relationships between fields.

Formatting does not alter the data. A formatted JSON document and its minified counterpart are semantically identical — they parse to the exact same object. The only difference is whitespace outside of string values, which the JSON specification explicitly defines as insignificant.

Why Format JSON? 5 Real-World Use Cases

1. Debugging API Responses

REST and GraphQL APIs often return minified JSON to save bandwidth. When you need to inspect the response — checking whether a nested field exists, verifying data types, or tracing a bug — formatted JSON lets you scan the structure at a glance instead of scrolling through a wall of text.

2. Reviewing Configuration Files

Files like package.json, tsconfig.json, and .eslintrc.json are central to every project. Proper formatting makes code reviews faster — reviewers can immediately spot what changed in a diff rather than mentally parsing unindented JSON.

3. Writing Documentation

API docs, tutorials, and README files need readable JSON examples. A JSON beautifier ensures consistent indentation across all your examples, which improves readability and professionalism.

4. Log Analysis

Structured logging often produces single-line JSON per event. When investigating an incident, formatting the JSON helps you spot anomalies — missing fields, unexpected nulls, or malformed nested objects — that would be invisible in compact form.

5. Data Exploration

When you receive a JSON dataset for the first time (from an API, database export, or third-party feed), formatting it is the fastest way to understand the schema. You see field names, data types, and nesting levels without writing any code.

How to Format JSON in Every Language

Every modern programming language has built-in support for JSON pretty print. Here are the most common approaches, from JavaScript to command-line tools.

JavaScript / TypeScript

// 2-space indent (most common)
const formatted = JSON.stringify(data, null, 2);

// 4-space indent
const formatted4 = JSON.stringify(data, null, 4);

// Tab indent
const formattedTab = JSON.stringify(data, null, "\t");

// Format a JSON string (parse then re-stringify)
const pretty = JSON.stringify(JSON.parse(rawString), null, 2);

Python

import json

# Format a Python dict
formatted = json.dumps(data, indent=2)

# Sort keys alphabetically
formatted = json.dumps(data, indent=2, sort_keys=True)

# Format a JSON file
with open("data.json") as f:
    data = json.load(f)
with open("data.json", "w") as f:
    json.dump(data, f, indent=2)

# Command line: python -m json.tool input.json

Go

import "encoding/json"

// From a struct or map
b, _ := json.MarshalIndent(data, "", "  ")
fmt.Println(string(b))

// From a raw JSON byte slice
var buf bytes.Buffer
json.Indent(&buf, rawBytes, "", "  ")

Command Line (jq)

# Format a JSON file
jq '.' data.json

# Format piped input
curl -s https://api.example.com/data | jq '.'

# Format with 4-space indent
jq --indent 4 '.' data.json

# Format and sort keys
jq -S '.' data.json

Java

// Jackson
ObjectMapper mapper = new ObjectMapper();
String pretty = mapper
    .writerWithDefaultPrettyPrinter()
    .writeValueAsString(data);

// Gson
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String pretty = gson.toJson(data);

C# / .NET

// System.Text.Json
var options = new JsonSerializerOptions { WriteIndented = true };
string pretty = JsonSerializer.Serialize(data, options);

// Newtonsoft.Json
string pretty = JsonConvert.SerializeObject(data, Formatting.Indented);

JSON Indentation — Which Style Should You Use?

StyleUsed ByBest For
2 Spacesnpm, VS Code, Google Style Guide, most APIsGeneral use — compact yet readable
4 SpacesPython (PEP 8), some Java projectsDeep nesting, matching Python code style
TabGo (gofmt), WordPress, some legacy projectsTab-indented codebases, accessibility
None (minified)API payloads, localStorage, wire formatProduction — saves bandwidth and storage

JSON Formatter vs. JSON Minifier

Formatting and minifying are opposite operations. A JSON formatter expands compact JSON into indented, readable output. A JSON minifier strips all whitespace to produce the smallest possible payload. Both produce semantically identical JSON.

Formatted (72 bytes)

{
  "name": "Alice",
  "age": 30,
  "active": true
}

Minified (38 bytes)

{"name":"Alice","age":30,"active":true}

Formatting increased the size by 89% — that overhead matters for API payloads but is irrelevant for config files and debugging. Use formatting during development and minification in production.

Common JSON Errors That Break Formatting

A JSON formatter can only format valid JSON. If your JSON has syntax errors, the formatter will reject it. Here are the most common mistakes and how to fix them:

ErrorExampleFix
Trailing comma{"a": 1,}Remove the comma after the last value
Single quotes{'name': 'Alice'}Replace with double quotes
Unquoted keys{name: "Alice"}Wrap keys in double quotes
undefined values{"x": undefined}Use null, or remove the field
Comments// this is a commentJSON does not allow comments — remove them

This formatter highlights the exact error line so you can fix syntax issues before formatting. For more help, see our Unexpected Token guide and JSON Parse Error guide.

Frequently Asked Questions

How do I format JSON in JavaScript?

Use JSON.stringify(obj, null, 2) for 2-space indentation, or JSON.stringify(obj, null, '\t') for tabs. The third argument controls the indent style. For formatting a JSON string, parse it first: JSON.stringify(JSON.parse(jsonString), null, 2).

How do I format JSON in Python?

Use json.dumps(obj, indent=2) for 2-space indentation. Add sort_keys=True to alphabetize keys. For files: json.dump(obj, open('file.json', 'w'), indent=2). From the command line: python -m json.tool input.json.

What is the difference between a JSON formatter and a JSON validator?

A JSON validator checks whether JSON syntax is correct (balanced braces, proper quoting, valid values). A JSON formatter takes valid JSON and adds indentation and line breaks for readability. Most formatters, including this one, validate first and then format — so you get both in one step.

Does formatting change the data in my JSON?

No. Whitespace outside of string values is insignificant in JSON. A formatted document and its minified equivalent are semantically identical — they parse to the exact same data structure. Formatting only affects human readability.

How do I format JSON from the command line?

Use jq: echo '{"a":1}' | jq '.'. In Python: python -m json.tool file.json. In Node.js: node -e "console.log(JSON.stringify(JSON.parse(require('fs').readFileSync('/dev/stdin','utf8')),null,2))" < file.json.

Should I use 2 spaces, 4 spaces, or tabs for JSON indentation?

2 spaces is the most common convention for JSON files (used by npm, VS Code defaults, and most APIs). 4 spaces is common in Python ecosystems. Tabs are rare in JSON but can be useful for consistency with tab-indented codebases. The JSON spec does not mandate any particular style.

Related JSON Tools

Format JSON is just the start. Explore the full toolkit:

If jsondecode.com saved you time, share it with your team

Free forever. No ads. No sign-up. Help other developers find it.