UnblockDevs

JSON Formatter Guide — Format, Validate, Auto-Fix & Minify JSON in One Tool

A proper JSON formatter does much more than add indentation. It validates syntax, pinpoints errors with exact line and column numbers, auto-fixes common mistakes like trailing commas and single quotes, and can minify output for production. This guide covers what to look for in a JSON formatter, how to use each feature, and the full workflow from raw API response to clean, validated JSON.

Format

Pretty-print with 2-space, 4-space, or tab indentation

Validate

Syntax checking with exact line:column error positions

Auto-fix

Trailing commas, single quotes, unquoted keys — repaired automatically

1

What a Good JSON Formatter Does

Not all JSON formatters are equal. A basic one just adds indentation. A good one validates the structure, explains errors, and helps you fix them without leaving the tool.

Format with configurable indent

Converts compressed single-line JSON to indented multi-line output. Supports 2-space (JavaScript standard), 4-space (Python/enterprise), and tab indent. Lets you switch between formats instantly.

Validate syntax in real time

Highlights syntax errors as you type. Shows the exact line number and column where the parser failed. Good formatters also suggest the likely fix — not just where the error is.

Auto-fix common mistakes

Trailing commas, single-quoted strings, unquoted keys, and JavaScript comments are all invalid JSON but extremely common. Auto-fix converts these to valid JSON automatically.

Minify to compact output

Strips all whitespace to produce the smallest valid JSON string. Essential for production API payloads, cache values, and message queue messages where size matters.

2

Format JSON — From Raw to Readable

The most common use case: you have a compressed API response or log line and need to read it. Formatting adds the indentation that makes nesting, arrays, and field names visible.

bashTypical raw API response — needs formatting
# curl output — one long line, hard to read
curl -s https://api.example.com/users/42
{"id":42,"name":"Alice Smith","email":"alice@example.com","role":"admin","permissions":["read","write","delete"],"lastLogin":"2024-11-15T10:30:00Z","profile":{"avatar":"https://cdn.example.com/avatars/42.jpg","bio":"Senior Developer","timezone":"Europe/London","company":{"id":7,"name":"Acme Corp","plan":"enterprise"}}}
jsonAfter formatting with 2-space indent
{
  "id": 42,
  "name": "Alice Smith",
  "email": "alice@example.com",
  "role": "admin",
  "permissions": [
    "read",
    "write",
    "delete"
  ],
  "lastLogin": "2024-11-15T10:30:00Z",
  "profile": {
    "avatar": "https://cdn.example.com/avatars/42.jpg",
    "bio": "Senior Developer",
    "timezone": "Europe/London",
    "company": {
      "id": 7,
      "name": "Acme Corp",
      "plan": "enterprise"
    }
  }
}

Format curl output directly in the terminal

Pipe curl output through Python for instant formatting without leaving the terminal: curl -s https://api.example.com/users/42 | python3 -m json.tool. Or use jq . if jq is installed: curl -s https://api.example.com/users/42 | jq .. For a GUI with copy, download, and tree view: use the online JSON Formatter.
3

Validate JSON — Find Errors Before They Hit Production

JSON validation catches syntax errors before your code does. A validation error in a config file or API payload causes cryptic runtime errors — finding it at the source saves significant debugging time.

jsonCommon validation errors with explanations
// Error: Trailing comma after last property
{
  "name": "Alice",
  "age": 30,    ← SyntaxError: Unexpected token } in JSON at position 34
}

// Error: Missing comma between properties
{
  "name": "Alice"    ← SyntaxError: Expected comma or }
  "age": 30
}

// Error: Unquoted key
{
  name: "Alice",    ← SyntaxError: Expected property name or }
  "age": 30
}

// Error: Incorrect nesting — array closed with }
{
  "users": [
    {"id": 1, "name": "Alice"},
    {"id": 2, "name": "Bob"}
  }    ← SyntaxError: Expected , or ] — mismatched bracket
}

Error messages point to where the parser got confused, not where the bug is

JSON parse errors report the position where the parser gave up — not where you made the mistake. A missing comma on line 5 might cause a parse error reported on line 6 when the parser hits an unexpected character. Always look a few lines before the reported error position for the actual mistake.

4

Auto-Fix — Repair JavaScript-Style JSON Automatically

Developers often write JSON the same way they write JavaScript object literals — with trailing commas, single quotes, unquoted keys, and comments. These are all invalid in strict JSON. The auto-fix feature handles all of them in one step.

JavaScript object literal vs valid JSON

JavaScript syntax — 4 JSON violations, fails in any JSON parser

❌ Bad
// This is valid JavaScript, but NOT valid JSON:
{
  // User config
  name: 'Alice',         // unquoted key, single-quoted string
  age: 30,              // trailing comma
  roles: ['admin',],    // trailing comma in array
  active: true,
}

Valid JSON — all keys quoted, double quotes, no trailing commas, no comments

✅ Good
{
  "name": "Alice",
  "age": 30,
  "roles": ["admin"],
  "active": true
}
javascriptAuto-fix in code — safe parser libraries
// json5 — parses JSON5 superset (comments, single quotes, trailing commas)
import JSON5 from 'json5';
const parsed = JSON5.parse(`{
  // config
  name: 'Alice',
  roles: ['admin',],
}`);
// Returns { name: 'Alice', roles: ['admin'] }

// json-parse-even-better-errors — better error messages for debugging
import parseJson from 'json-parse-even-better-errors';
try {
  parseJson('{"name": "Alice",}');
} catch (e) {
  console.log(e.message);
  // JSON5: invalid character '}' at 1:18 — more actionable than native
}
5

Minify JSON — Optimize for Production

Minified JSON is compact JSON with all non-essential whitespace removed. For large API payloads and frequently cached data, minification meaningfully reduces transfer time and storage costs.

jsonPretty vs minified — size comparison
// Pretty-printed: 245 bytes
{
  "id": 42,
  "name": "Alice Smith",
  "email": "alice@example.com",
  "roles": [
    "admin",
    "editor"
  ],
  "active": true
}

// Minified: 83 bytes (66% smaller)
{"id":42,"name":"Alice Smith","email":"alice@example.com","roles":["admin","editor"],"active":true}
javascriptMinify JSON safely in code
// Safe: parse then re-stringify without space parameter
const minified = JSON.stringify(JSON.parse(prettyJson));

// Node.js script: minify a JSON file
import fs from 'fs';
const data = JSON.parse(fs.readFileSync('./data.json', 'utf8'));
fs.writeFileSync('./data.min.json', JSON.stringify(data));

// Express middleware: always send minified JSON
app.use((req, res, next) => {
  res.json = (obj) => {
    res.type('json').send(JSON.stringify(obj)); // no pretty-print
  };
  next();
});
6

JSON Formatter in Development Workflows

1

Inspect API responses during development

Copy response body from browser DevTools Network tab → paste into JSON Formatter → instantly see nested structure, array sizes, and data types. Faster than reading compressed output.

2

Validate JSON config files before deploying

Paste your package.json, tsconfig.json, docker-compose.json, or CI pipeline config into the formatter before committing. Catch trailing commas and syntax errors before they break a build.

3

Format test fixtures for readability

JSON test fixtures committed to your repo should be pretty-printed for code review readability. Format all fixtures consistently and configure Prettier to maintain that format automatically.

4

Debug serialization bugs in API integrations

When an API integration returns unexpected data, format the raw response to see the actual field names (snake_case vs camelCase), unexpected null values, and nested array structures that may differ from documentation.

Use the JSON Formatter + Beautifier at unblockdevs.com/json-beautifier

The UnblockDevs JSON workbench combines formatting, validation, auto-fix, tree view, and TypeScript/SQL generation in one tool. Paste your JSON once and get everything you need.

Frequently Asked Questions