JSON Formatter and Validator
Errors that tell you what to fix.
Errors that tell you what to fix.
How to use it
- Paste the JSON. Valid or not. If it will not parse, the error says where and why.
- Choose the indent. Two spaces, four, tabs, or minified for the smallest output.
- Copy the result. Sort keys first if you are about to compare two documents.
When you would use this
Formatting JSON is a single call to the standard library. Everything worth having in a JSON tool is in what it says when the input will not parse. The browser's own message is not much help. Depending on the input and the browser you get either an offset into a string you cannot see, or a truncated echo of your own document with no position at all. Neither tells you what to change. This scans the source for the mistake instead. A trailing comma before a closing bracket, a single quoted string, an unquoted property name: those three account for most invalid JSON, all three are perfectly legal JavaScript, and that is precisely why people write them. Each one is reported with the line and column of the exact character to change. A truncated document gets its own message too, because unexpected end of input does not tell anyone their file was cut off. Sorting keys is offered because it is the thing that makes two JSON documents diffable. Arrays keep their order, since the order of an array is data. Nothing is uploaded, which matters here more than for most tools: the JSON people paste into a formatter is usually a real API response.
Questions
- Why are the errors more useful than the browser's?
- Because the source is scanned for the mistake rather than the parser's message being repeated. A trailing comma, a single quoted string and an unquoted property name account for most invalid JSON, all three are legal JavaScript, and each is named with the line and column of the character to change.
- What does sorting keys do to arrays?
- Nothing. The order of an array is data rather than presentation, so it is left exactly as it is. Only object keys are sorted, which is what makes two documents comparable when a serialiser somewhere emitted the same fields in a different order.
- Is my JSON sent anywhere?
- No. It runs in the page. This matters more for JSON than for most things, because the JSON people paste into a formatter is routinely an API response with real customer records still in it.