JSON Formatter and Validator, Explained
After reading this you will be able to format messy JSON, read a parser error down to the exact column, minify safely for production, and pull values out of a document with JSONPath.
What JSON is and one example
JSON (JavaScript Object Notation) is a text format for structured data, defined by RFC 8259 and ECMA-404. It has exactly six kinds of value: object, array, string, number, boolean and null. That is the entire grammar. An object is an unordered set of "key": value pairs wrapped in braces. An array is an ordered list wrapped in brackets.
Here is a document you will see again below, minified onto one line:
{"store":{"book":[{"title":"Sayings of the Century","price":8.95},{"title":"Moby Dick","price":8.99}]}}
A formatter turns that into something a person can read. A validator confirms the bytes actually parse. A JSONPath query, such as $.store.book[0].title, returns the single value "Sayings of the Century". All three jobs run against the same in-memory parse tree, so the tool does them at once.
When to use it, and when not
Reach for this tool when you have JSON as text and need to answer one of four questions: is it valid, what does it look like indented, how small can it get, and what value sits at a given path. Log lines, API responses pasted from your browser network tab, and config files are the common cases.
Do not use a JSON formatter on JSON5, JSONC, or "JSON with comments and trailing commas". Those are different grammars. RFC 8259 forbids comments and trailing commas, so a strict validator will reject them by design. If your file is really YAML or TOML, convert it first.
JSON numbers are decimal and have no declared precision. Many parsers read them as IEEE 754 doubles, which hold about 15 to 17 significant decimal digits. An integer like 9007199254740993 (that is 2^{53}+1) cannot be stored exactly as a double and may round to 9007199254740992. If you carry 64-bit IDs, keep them as strings.
How the parser reports position
A JSON parser reads one character at a time and tracks two counters: the line number and the column within that line. When it meets a character the grammar does not allow, it stops and reports where. The tool quotes the offending text so you can see it in context.
The column count is the important detail. Most tools count columns starting at 1 and count a tab as one column, not eight. So a message like Unexpected token at line 3, column 14 means: go to the third line, count 14 characters from the left, and look there. The error is usually the character just before that point, because the parser only knows something is wrong once it has read one character too many.
The four operations, precisely
Validate answers a yes or no question: does the byte string match the JSON grammar? There is no partial credit. A single stray comma makes the whole document invalid.
Format (also called pretty-print) parses the document, then re-serializes it with newlines and a chosen indent. Two spaces and four spaces are the common choices. Formatting never changes the data, only the whitespace between tokens.
Minify is the inverse: parse, then serialize with no whitespace at all between tokens. For the store example above, the readable form spans 9 lines and about 180 bytes; the minified form is 98 bytes, a 46 percent reduction. Whitespace inside strings is preserved either way, because it is data.
Sort keys re-serializes object members in ascending code-point order of their keys. JSON objects are defined as unordered, so sorting is safe for meaning and useful for diffing. Sorting is stable and recursive: nested objects are sorted too. Array order is never touched, because arrays are ordered.
JSONPath, the query language
JSONPath walks the parse tree using a small set of selectors. The root is $. From there you descend by key or by index.
.keyor['key']- Select a child by name. Bracket form is required when the key has spaces or dots.
[0]- Select an array element by zero-based index.
[0:2]- Slice: elements 0 and 1, following the Python half-open convention where the end index is excluded.
*- Wildcard: every child of the current node.
..key- Recursive descent: find
keyat any depth below the current node.
A query returns a list of matches, not a single value, because a wildcard or recursive descent can match many nodes. Even $.store.book[0].title, which matches exactly one node, comes back as a one-element list.
Reproducing the demo data
Load the demo, which is the store document from the first section. Format it with two-space indent and you get:
{ "store": { "book": [ { "title": "Sayings of the Century", "price": 8.95 }, { "title": "Moby Dick", "price": 8.99 } ] } }
- Query
$.store.book[0].title. The parser starts at the root, entersstore, thenbook, takes index 0, then readstitle. Result:["Sayings of the Century"]. - Query
$.store.book[*].price. The wildcard visits both books. Result:[8.95, 8.99]. - Query
$..price. Recursive descent finds everypriceanywhere below the root. Same result here:[8.95, 8.99]. - Query
$.store.book[0:1].title. The slice keeps index 0 only, so the result is["Sayings of the Century"].
Now check the size numbers. The minified document is 98 bytes. Formatted at two spaces it is 180 bytes; at four spaces, 214 bytes. The extra indentation costs 34 bytes for two extra spaces on 17 indented lines.
Common mistakes
Trailing commas. [1, 2, 3,] is valid in JavaScript source but invalid JSON. RFC 8259 allows no comma after the last element. This is the single most common parse failure.
Single quotes. JSON strings must use double quotes. {'a': 1} fails; {"a": 1} passes.
Unquoted keys. {a: 1} is a JavaScript object literal, not JSON. Keys are always double-quoted strings.
Comments. Neither // nor /* */ is legal JSON. Strip them before validating.
Leading zeros and bare dots. 012 and .5 are invalid; write 12 and 0.5. A number may not have a leading zero unless it is zero itself.
Duplicate keys are a trap. {"a": 1, "a": 2} parses without error under RFC 8259, which does not forbid duplicates, but the result is undefined: most parsers keep the last value, so you get 2 and silently lose 1. Sort keys will not warn you either. Check for duplicates deliberately.
Related tools
To move data between formats, use the JSON, YAML, TOML, XML and CSV converter. To compare two documents by structure rather than by text, use JSON Diff. When your JSON holds embedded strings that need escaping or unescaping, the String Escaper handles JSON string rules directly. If you are matching JSON with regular expressions (rarely a good idea, but sometimes needed for logs), test them in the Regex Tester. For SQL that returns or stores JSON, the SQL Formatter pretty-prints the query, and the .gitignore Generator helps keep large formatted fixtures out of version control.
Frequently asked questions
Why does my JSON fail with an error at the very end of the file?
Usually an opening brace or bracket was never closed. The parser reads to the end still expecting more, then reports the failure at the last position. Count your { against } and [ against ].
Does formatting change my numbers?
Formatting preserves the number tokens as written. Some tools do re-serialize numbers, which can turn 1.0 into 1 or 1e2 into 100. The mathematical value is the same, but the text differs, which matters for byte-exact comparisons.
What does a JSONPath query return when nothing matches?
An empty list, not an error. A query for $.store.magazine against the demo returns [] because there is no magazine key.
Is sorting keys safe to commit?
Yes for meaning, since JSON objects are unordered. It is very useful before a diff because it removes noise from reordered keys. Do not sort if some consumer wrongly depends on key order.
Does anything I paste get uploaded?
No. Parsing, formatting, minifying and querying all run in your browser. Nothing leaves your device.