Skip to tool

JSON Formatter

Input
Output

Formatted JSON and a collapsible tree

Format, validate and view JSON

The JSON Formatter parses the JSON you paste, reports whether it is valid, and shows it in two views: a formatted view with syntax colors and a tree view in which objects and arrays can be collapsed. The formatted view can show the JSON indented or minified, ready to copy. Typical inputs are API responses, configuration files and test fixtures. It does not edit values, sort keys or convert to other formats.

How parsing and formatting work

Validation uses the browser's JSON.parse, which implements the grammar in RFC 8259 and ECMA-404, and runs again on every change. Comments, trailing commas, single quotes, unquoted keys, NaN and undefined are rejected. When parsing fails, the tool scans the text again to find the first error and reports its line, column and a likely fix. While the input is invalid, the last valid output stays visible, dimmed.

Format and Minify serialize the parsed value again with JSON.stringify, with two-space indentation or no whitespace. The output therefore reflects how JavaScript read the input, not the original characters: 1.0 becomes 1 and 1e3 becomes 1000.

The tree view walks the parsed value recursively. A collapsed array shows its item count, a collapsed object shows its key count, and array elements are labeled with zero-based indexes.

Example: expanding a minified object

Input, one line of 62 characters: {"id":7,"tags":["a","b"],"owner":{"name":"Ada","active":true}}

The Format view shows it as 11 lines and 103 characters, with each key on its own line and two spaces of indentation per level. The Minify view shows the original line. In the Tree tab, collapsing tags shows [ 2 items ] and collapsing owner shows { 2 keys }.

If you delete the final brace, the status changes to Line 1, col 62 and the message says the text ends too early, because the parser still expected a comma or a closing brace.

Limitations of this formatter

  • Only strict JSON is accepted. JSON5, JSONC (with comments) and JSON Lines are reported as invalid.
  • Numbers are 64-bit floats. Integers beyond ±(2^53 − 1), such as 64-bit IDs, lose precision when formatted or copied: 12345678901234567890 becomes 12345678901234567000.
  • If an object repeats a key, only the last value is kept. RFC 8259 says names SHOULD be unique but does not forbid duplicates.
  • Integer-like keys such as "10" move in front of other keys in numeric order, following JavaScript's property order.
  • Copy copies the view that is shown: formatted or minified text. In the Tree tab it copies the formatted text.

JSON formatting questions

Why is my JSON invalid when the same text works in JavaScript?

JavaScript object literals allow unquoted keys, single quotes, trailing commas, comments and undefined. JSON allows none of these: keys and strings need double quotes, and values are limited to strings, numbers, true, false, null, objects and arrays.

How do I fix an "Unexpected token" error in JSON?

The message names the character the parser did not expect and, in most browsers, its position. Go to that position and look just before it. The usual causes are a trailing comma before } or ], a single-quoted string or key, an unquoted key, a // or /* */ comment, and text copied with smart quotes (“ ”) instead of straight double quotes. Fix the first error, and the parser moves on to the next one.

What is the difference between Format and Minify?

Format adds line breaks and indentation for reading. Minify removes all whitespace outside strings for the shortest text. Both represent the same data.

Why do \u escapes such as \u4e2d turn into characters after formatting?

JSON.parse decodes \uXXXX escapes, and JSON.stringify writes non-ASCII characters as they are, so "\u4e2d\u6587" appears as "中文" in the output. Both forms are the same string. Characters that JSON must escape, such as double quotes, backslashes and control characters like \n, stay escaped.

How do I pretty-print JSON in JavaScript, Python or the command line?

JavaScript: JSON.stringify(value, null, 2). Python: json.dumps(data, indent=2). In a shell, python -m json.tool file.json indents with 4 spaces by default, and jq . file.json indents with 2.

References