You call an API and get back one long line of text. Somewhere in it is a mistake, and your code says "Unexpected token , in JSON at position 4291". Counting to character 4,291 by hand is not a good use of time.
A JSON formatter helps in two ways. It makes valid JSON readable, and it tells you when the JSON is invalid. Our free JSON Formatter and Validator does both in your browser. This guide explains how to use it and how to fix the most common errors.
What the tool does
- Beautify: adds line breaks and indentation, with either 2 or 4 spaces.
- Minify: removes all extra spaces and line breaks, and shows how much smaller the result is.
- Validate only: checks the JSON and tells you if it is valid.
- Error message: if the JSON is invalid, the tool shows the error message that your browser gives. This usually includes a position or the unexpected character.
- Extras: a sample button, a copy button, and a count of characters and kilobytes for the input and output.
It does not repair broken JSON, and it does not have a collapsible tree view. It shows plain formatted text.
Is it safe to paste JSON into an online tool?
It depends on the tool. Some websites send your text to a server. Ours reads and formats your JSON in your own browser, so it is not sent to our server. Even so, it is a good habit never to paste live passwords, secret keys or private customer data into any online tool. Replace real values with dummy ones first.
Beautify or minify?
- Beautify when a person needs to read it: debugging an API response, checking a config file, or reviewing a change.
- Minify when size matters: sending data over a network, or storing it compactly.
The most common JSON errors
JSON is strict. It is based on JavaScript, but some things that JavaScript allows are not valid JSON. The rules are defined in RFC 8259.
- A trailing comma. A comma after the last item, as in
{"name": "AioGalaxy",}, is not allowed in JSON. - Single quotes. Strings and keys must use double quotes.
'key'is not valid. - Unquoted keys.
{status: 200}is valid in JavaScript, but in JSON the key must be"status". - Comments. JSON does not allow
//or/* */comments. - Special values.
undefined, functions andNaNare not valid JSON values. The allowed ones are objects, arrays, strings, numbers, true, false and null. - Unescaped characters. A double quote or a raw line break inside a string must be escaped, for example
\"or\n.
How to find the error
- Paste your JSON and press Validate Only.
- Read the message. It often names the unexpected character and its position.
- Look just before that position. The real mistake, such as a missing comma, is often right before the place where the parser gives up.
- Fix it and validate again. Repeat until it says valid.
- Then press Beautify to read the result easily.
Things to know about the result
The tool uses your browser's own JSON parser and then writes the data back out. This has two side effects. Very large numbers, beyond what JavaScript can store exactly, can lose precision. And if a key appears twice in the same object, only the last value is kept. If either matters for your data, compare the output with the original.
Also, the order of keys is generally kept, but do not rely on key order in JSON, because the format does not guarantee it.
A quick habit that saves time
When you work with APIs, validate any JSON you write by hand before you send it. A few seconds in a validator is cheaper than a failed request and a confusing error from the server.
If you need to compare two versions of a file, our WebTools also include a diff checker.
Comments
No comments yet — be the first to share your thoughts.