ToolPal
Close-up of a computer monitor displaying cyber security data and code, indicative of system hacking or programming.

Stop Wrestling With Messy JSON: Format It in 2 Seconds (Free)

📷 Tima Miroshnichenko / Pexels

Stop Wrestling With Messy JSON: Format It in 2 Seconds (Free)

Paste unreadable JSON, get clean formatted output instantly. No installs, no signups — the fastest way to debug API responses and config files.

DBy Daniel ParkMarch 15, 20269 min read

What is JSON?

JSON (JavaScript Object Notation) is a lightweight data interchange format that's easy for humans to read and write, and easy for machines to parse and generate. It's the most popular format for API responses, configuration files, and data storage in modern web development.

If you work with APIs at all, you deal with JSON constantly. Every REST API response, every webhook payload, every configuration file in a Node.js project -- the odds are high that it is JSON. Understanding how to read, format, and debug JSON is a core developer skill.

Why Format JSON?

Minified JSON is difficult to read. API responses often arrive as a single long line with no whitespace, which makes debugging a nightmare. Formatting (or "pretty-printing") JSON adds proper indentation and line breaks, making the structure clear and easy to understand.

Before Formatting:

{"name":"John","age":30,"city":"New York","hobbies":["reading","coding"],"address":{"street":"123 Main St","zip":"10001"}}

After Formatting:

{
  "name": "John",
  "age": 30,
  "city": "New York",
  "hobbies": [
    "reading",
    "coding"
  ],
  "address": {
    "street": "123 Main St",
    "zip": "10001"
  }
}

The difference is dramatic. In the formatted version, you can immediately see that there are five top-level keys, that hobbies is an array with two items, and that address is a nested object. In the minified version, you would have to visually parse matching braces and brackets to figure any of that out.

How to Use Our JSON Formatter

  1. Paste your JSON into the input field
  2. Click "Format" to beautify with 2 or 4 spaces
  3. Click "Minify" to compress JSON into a single line
  4. Copy the result with one click

The tool validates your JSON as you paste it, so you get instant feedback if something is wrong.

Common JSON Errors (and How to Fix Them)

JSON has strict syntax rules, and violating any of them produces a parse error. Here are the most common mistakes, ordered by how often I see them in practice:

1. Trailing Commas

This is by far the most common JSON error. JavaScript allows trailing commas in arrays and objects, so developers often forget that JSON does not.

// INVALID -- trailing comma after "coding"
{
  "hobbies": ["reading", "coding",]
}

// VALID
{
  "hobbies": ["reading", "coding"]
}

2. Single Quotes Instead of Double Quotes

JSON requires double quotes for all strings. Single quotes, which are valid in JavaScript and Python, will break JSON parsing.

// INVALID
{'name': 'John'}

// VALID
{"name": "John"}

3. Missing Quotes Around Keys

In JavaScript, object keys do not need quotes (unless they contain special characters). In JSON, every key must be a double-quoted string.

// INVALID
{name: "John", age: 30}

// VALID
{"name": "John", "age": 30}

4. Comments in JSON

JSON does not support comments. Period. This catches people off guard because JSON looks like JavaScript, and JavaScript supports comments. If you need comments in a config file, consider using JSONC (JSON with Comments, supported by VS Code) or YAML instead.

// INVALID
{
  "port": 3000,  // server port
  "host": "localhost"
}

// VALID
{
  "port": 3000,
  "host": "localhost"
}

5. Unescaped Special Characters

Strings containing backslashes, double quotes, or control characters must be properly escaped:

// INVALID -- unescaped backslash and quotes
{"path": "C:\Users\john", "note": "He said "hello""}

// VALID
{"path": "C:\\Users\\john", "note": "He said \"hello\""}

6. Invalid Values

JSON supports exactly these value types: string, number, boolean (true/false), null, object, and array. Anything else is invalid:

// INVALID
{"value": undefined}
{"count": NaN}
{"total": Infinity}
{"date": new Date()}

// VALID alternatives
{"value": null}
{"count": 0}
{"total": 9999999}
{"date": "2026-03-15T10:30:00Z"}

JSON Formatting in the Command Line

Sometimes you do not have access to a web-based tool, or you need to format JSON as part of a script. Here are the most useful command-line approaches:

Using Python (available on most systems)

# Format a JSON file
python3 -m json.tool input.json

# Format JSON from a pipe
curl -s https://api.example.com/data | python3 -m json.tool

# Format with custom indentation
python3 -c "import json,sys; print(json.dumps(json.load(sys.stdin), indent=4))" < input.json

Using jq (the Swiss army knife for JSON)

jq is a dedicated JSON processor and arguably the most powerful tool for working with JSON on the command line:

# Pretty print
cat data.json | jq '.'

# Format and extract a specific field
cat data.json | jq '.users[0].name'

# Format API response
curl -s https://api.example.com/users | jq '.'

If you work with JSON regularly and do not have jq installed, stop reading and install it now. It will save you hours.

Using Node.js

# Quick format
node -e "console.log(JSON.stringify(JSON.parse(require('fs').readFileSync('/dev/stdin','utf8')),null,2))" < input.json

Tips for Working with JSON

Choose Your Indentation Wisely

  • 2-space indentation: More compact, fits more on screen. Popular in JavaScript/TypeScript projects and most web frameworks. This is what most API documentation uses.
  • 4-space indentation: Easier to read for deeply nested structures. Common in Python projects and enterprise Java environments.
  • Tab indentation: Technically valid but rare in JSON. Stick with spaces.

For most web development, 2 spaces is the pragmatic choice. It gives enough visual hierarchy without wasting horizontal space.

Validate Before You Ship

Always validate JSON before using it in production, especially if it is hand-written or modified by a script. A single missing comma or extra bracket can bring down an entire application. Our formatter catches syntax errors as you paste, which makes it a quick way to sanity-check your data.

Use JSON Schema for Structured Validation

If you are building an API that accepts JSON input, raw parsing is not enough. JSON Schema lets you define the expected structure, types, and constraints:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": { "type": "string", "minLength": 1 },
    "age": { "type": "integer", "minimum": 0 },
    "email": { "type": "string", "format": "email" }
  },
  "required": ["name", "email"]
}

This catches issues like missing required fields, wrong types, or values outside acceptable ranges -- things that basic JSON parsing would not flag.

Sorting Keys for Consistency

When comparing two JSON objects (for example, in code review or config diffs), unsorted keys make the diff noisy. Many formatters, including ours, offer a "sort keys" option that alphabetizes object keys recursively. This is especially useful for:

  • Configuration files in version control (reduces meaningless diffs)
  • API response comparison during debugging
  • Test snapshots that need to be deterministic

Real-World JSON Debugging Scenarios

Debugging API Responses

You fire off a curl request and get back a wall of minified JSON. Before you can understand what went wrong, you need to see the structure:

curl -s https://api.example.com/users/123 | jq '.'

Or paste the response into our JSON formatter. Now you can see whether the error is in the response structure, a missing field, or an unexpected value.

Comparing Two JSON Objects

You have two API responses that should be identical but something is different. Format both with sorted keys, then use a diff tool:

jq -S '.' response1.json > sorted1.json
jq -S '.' response2.json > sorted2.json
diff sorted1.json sorted2.json

The -S flag sorts keys alphabetically, so structural differences do not create noise in the diff.

Fixing Copy-Paste Errors

A colleague pastes some JSON into Slack, and the smart quotes mangle it. You paste it into a formatter, it highlights the error (curly quotes instead of straight quotes), and you know exactly what to fix.

This happens more often than you might think. Chat applications, word processors, and even some text editors will silently replace " with curly quotes like \u201c and \u201d, which are invalid in JSON.

Extracting Data from Large JSON Files

Sometimes you get a JSON file that is megabytes of data and you only need a small piece. Rather than scrolling through thousands of lines, use jq to extract exactly what you need:

# Get the first user's email
cat users.json | jq '.data.users[0].email'

# Get all user names as a flat list
cat users.json | jq '[.data.users[].name]'

# Filter users by status
cat users.json | jq '[.data.users[] | select(.status == "active")]'

JSON vs Other Data Formats

FeatureJSONYAMLXMLTOML
ReadabilityGoodExcellentModerateGood
CommentsNoYesYesYes
Data typesBasicRichString-basedRich
File sizeSmallSmallLargeSmall
Parsing speedFastModerateSlowFast
EcosystemMassiveLargeLargeGrowing

JSON wins for API communication and data interchange because of its simplicity and universal parser support. YAML is better for configuration files where comments and readability matter. XML still has its place in enterprise systems and document formats. TOML is gaining traction for configuration files (Rust's Cargo, Python's pyproject.toml).

Security Considerations

Never Paste Sensitive Data into Server-Side Formatters

Some online JSON formatters send your data to a server for processing. If you are working with API keys, tokens, passwords, or any personal data, only use client-side formatters that process everything in your browser. Our tool does all formatting locally -- nothing leaves your machine.

Watch Out for JSON Injection

If you are building applications that accept JSON input, always validate and sanitize the data. Never construct JSON by string concatenation:

// DANGEROUS -- JSON injection risk
const json = '{"name": "' + userInput + '"}';

// SAFE
const json = JSON.stringify({ name: userInput });

Be Careful with JSON.parse on Untrusted Data

JSON.parse() is generally safe (unlike eval()), but the parsed data can still contain unexpected types or structures. Always validate the shape of parsed JSON before using it in your application.

Try our free JSON formatter to format your JSON data instantly! It runs entirely in your browser, supports syntax highlighting, and catches errors as you type.

Frequently Asked Questions

D

About the author

Daniel Park

Senior frontend engineer based in Seoul. Seven years of experience building web applications at Korean SaaS companies, with a focus on developer tooling, web performance, and privacy-first architecture. Open-source contributor to the JavaScript ecosystem and founder of ToolPal.

Learn more

Share this article

XLinkedIn

Related Posts