Tutorials

How to Format and Fix Invalid JSON (Free JSON Formatter)

Learn how to format, validate and minify JSON, what the most common syntax errors look like, and how to fix them, using real error messages from our tests.

ProTools Hub Team6 min read

What JSON is and why it is so strict

JSON (JavaScript Object Notation) is a text format for storing and exchanging data. APIs return it, configuration files use it, and apps save settings in it. It is built from objects in curly braces, arrays in square brackets, strings, numbers, true, false and null.

JSON looks like a JavaScript object, but the rules are tighter. Programs that read it expect it to match the specification exactly, so one stray comma can make the whole document unreadable. That is why a validator is a useful everyday tool.

Formatting, validating and minifying: what each does

The free [JSON Formatter & Validator](/tools/json-formatter) runs in your browser and offers these actions.

  • Format JSON adds line breaks and indentation so the structure is easy to read. You can choose 2 spaces, 4 spaces or a tab.
  • Minify removes all unnecessary whitespace so the text is as small as possible, which is handy for configuration values and network payloads.
  • Validate checks the syntax and tells you whether the JSON is valid.
  • Sort keys A-Z orders object keys alphabetically, which makes two documents easier to compare.
  • Load Sample, Upload .json and Clear help you get content in and out. Use as input, Download and Copy work on the result.

What we tested: valid JSON

We pasted a small valid document into the tool. It was 67 characters long. After clicking Format JSON with 2-space indentation, the tool reported that the JSON was valid and formatted successfully, and the output grew to 97 characters across 9 lines. That is expected, because indentation and line breaks add whitespace. We checked those figures independently, and they match.

The tool also showed a summary of the structure: 4 keys, a maximum depth of 3, 1 object, 1 array, 3 strings, 1 number, 1 boolean and no nulls. This is a quick way to confirm that a document contains what you expect.

Clicking Minify on the same kind of sample reported that the JSON was minified successfully. Our sample was already compact, so the output matched the input. This was a single small example, so treat it as a demonstration of how the tool behaves and not as a performance measurement.

What we tested: three common mistakes and the real error messages

Most JSON problems come from a few habits carried over from JavaScript or from hand editing. We tried three of them and recorded exactly what the tool reported.

  • Trailing comma. We entered {"a": 1, "b": 2,} (17 characters). The tool reported: Expected double-quoted property name in JSON at position 16 (line 1 column 17), with the note Problem near line 1, column 17. Position 16 is the closing brace, the place where the parser expected another key after the comma. The fix is to delete the comma after the last item.
  • Single quotes. We entered {'a': 1} (8 characters). The tool reported: Expected property name or '}' in JSON at position 1 (line 1 column 2). The parser stopped at the first single quote, because JSON requires double quotes around keys and strings. The fix is to replace every single quote with a double quote.
  • Missing closing brace. We entered {"a": 1, "b": [1, 2] (20 characters). The tool reported: Expected ',' or '}' after property value in JSON at position 20 (line 1 column 21). Position 20 is the end of the text, which is where the parser realized the object was never closed. The fix is to add the missing }.

How to read an error message

All three messages gave a character position and a line and column, and the tool added a reminder to check that spot and the line just before it. That reminder matters. The position is where the parser noticed the problem, which is not always where you made the mistake. The missing brace is the clearest example: the error points to the end of the text, but the real mistake is the object that was never closed.

The wording of the first part of each message comes from the browser's own JSON parser, so it may differ slightly between browsers. The line and column help you jump to the right area, and then you read nearby text for the cause.

Other mistakes to look for

Beyond the three we tested, these are frequent causes of invalid JSON.

  • Unquoted keys, such as {a: 1}. Keys must be in double quotes.
  • Comments. JSON does not allow // or /* */ comments.
  • Missing commas between items, or a comma where none belongs.
  • Values that are not allowed, such as undefined, NaN or a function.
  • Unescaped characters inside strings, such as a raw line break or a backslash that is not part of a valid escape.
  • Copying JSON from a document editor that replaced straight quotes with curly quotes.

A simple workflow for fixing broken JSON

When a document will not parse, work through it in this order.

  • Paste the text into the formatter and click Validate or Format JSON.
  • Read the line and column, then look at that spot and the line before it.
  • Fix one problem at a time and validate again, because a later error can be hidden behind an earlier one.
  • When it is valid, format it with your preferred indentation to review the structure.
  • Minify it if you need the compact version, then copy or download the result.

A note on privacy

The JSON Formatter processes text in your browser. Even so, avoid pasting secrets such as API keys, passwords or personal data into any online tool unless you are comfortable with that. Replace sensitive values with placeholders first. Our Privacy Policy explains what the site itself collects, including analytics and advertising scripts. To learn how to check what a browser tool does with your data, read [do browser-based tools upload your files](/blog/do-browser-based-tools-upload-your-files).

Try it free

JSON Formatter & Validator

Format, validate and minify JSON in your browser, with line and column numbers for syntax errors.

Open tool

Frequently asked questions

What is the difference between formatting and minifying JSON?

Formatting adds indentation and line breaks for readability. Minifying removes them to make the text smaller. The data stays the same.

Why does JSON not allow a trailing comma?

The JSON specification does not permit a comma after the last item. Many programming languages accept it, which is why it is such a common mistake.

Why does the error point to the end of my text?

Usually because something was never closed, such as a brace or bracket. The parser only notices at the end that it expected more.

Can I use single quotes in JSON?

No. Keys and strings must use double quotes.

Does it change my data when I format it?

Formatting changes whitespace only. If you turn on Sort keys A-Z, the order of keys changes, but the values stay the same.

Keep reading

Written by the ProTools Hub Team. This article is for general information. Features and browser support change over time, so check current documentation for anything critical.