JSON Formatter & Prettifier – DataMorph

Prettify unformatted JSON strings. Adjust indentation tabs, validate brackets, and colorize key structures.

What is JSON Formatter?

JSON (JavaScript Object Notation) is the dominant data interchange format for web APIs, configuration files, and data serialization. While JSON's syntax is relatively simple, minified or machine-generated JSON is difficult to read — all whitespace stripped, deeply nested structures on a single line, and no visual hierarchy. JSON formatting restores readability by adding consistent indentation, newlines between properties, and syntax coloring.

This JSON formatter validates and pretty-prints any valid JSON input with configurable indentation (2 spaces, 4 spaces, or tabs). The formatted output is immediately copy-ready and can be downloaded as a .json file. Invalid JSON is highlighted with specific error messages indicating the character position and nature of the syntax error to aid debugging.

JSON Syntax Rules and Common Errors

JSON has strict but simple syntax rules. All strings must use double quotes (not single quotes). Object keys must be quoted strings. Numbers cannot have leading zeros. Trailing commas after the last array element or object property are not allowed. undefined, NaN, Infinity, and JavaScript date objects are not valid JSON values. Comments are not part of the JSON specification — though JSON5 and JSONC extensions add comment support.

Common JSON errors include: unescaped control characters in strings (tabs, newlines must be \t, \n), unescaped backslashes (must be \\), unescaped quotation marks within string values (must be \"), missing commas between object properties, extra commas after final properties (trailing commas), and mixing single and double quotes. This formatter's validator provides specific error messages for each of these common mistakes.

JSON Schema and Structure Validation

Beyond syntax validation (is this valid JSON?), structural validation checks whether JSON data conforms to an expected schema — correct data types for each property, required properties present, array element types, and value constraints. JSON Schema (draft 2020-12) is the standard schema definition language for JSON, used by OpenAPI/Swagger API documentation, IDE autocomplete systems, and data pipeline validation layers.

Formatting JSON is frequently the first step in a debugging or development workflow: receive minified API response → format to understand structure → identify relevant fields → extract or transform data. This formatter supports the initial comprehension step, while complementary tools (JSON path tester, JSON schema validator) handle subsequent analysis and validation.

When Developers Use JSON Formatter

Frequently Asked Questions

What is the difference between JSON validation and JSON formatting?

Validation checks whether a string is syntactically valid JSON — correct quotes, brackets, commas, and value types. If validation fails, formatting cannot proceed. Formatting transforms valid JSON into a human-readable form with consistent indentation and newlines. Both operations are performed by this tool: it validates first, then formats if validation passes.

Why is trailing comma not allowed in JSON?

JSON is based on the 1999 ECMAScript specification which did not permit trailing commas. While modern JavaScript engines (ES5+) allow trailing commas in object and array literals, JSON remained strict to ensure compatibility with parsers in all programming languages and environments. JSON5 (a JSON superset) does allow trailing commas, but standard JSON parsers reject them.

What indentation should I use — 2 spaces, 4 spaces, or tabs?

Use whatever matches your project's coding style conventions. 2-space indentation is common in JavaScript/TypeScript and JSON API specifications. 4-space indentation is common in Python communities. Tab indentation uses a single tab character (variable visual width depending on editor settings). Most formatters and linters have configuration options to enforce project-specific preferences.

How do I format JSON in the terminal without this tool?

Linux/Mac: echo '{"a":1}' | python3 -m json.tool or echo '{"a":1}' | jq . (requires jq). Using Node.js: node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, 2))" '{"a":1}'. Using cat and python: cat file.json | python3 -m json.tool > formatted.json.

Why does my JSON fail to parse even though it looks correct?

Common hidden issues: BOM (Byte Order Mark) at start of file from Windows text editors, invisible Unicode characters in string values, Windows CRLF line endings causing issues in some parsers, string values containing unescaped control characters (tab/newline/null), or number values with NaN/Infinity which are JavaScript constructs not part of JSON. Check for BOM and use this tool's validator to identify the specific character position of errors.

Related Tools