How to fix invalid JSON

By · Updated

JSON is strict. One stray comma or the wrong kind of quote and the whole document is rejected, often with a message that points somewhere confusing. We fed six of the most common mistakes into our JSON Formatter and recorded exactly what came back, so you can match your error to its cause. The position in the message is where the parser gave up, which is often just after the real mistake.

The six errors we tested

Each message below is what the JSON Formatter reported in Chrome. The technical part comes from the browser, so its wording can differ slightly in other browsers; the tool adds the plain-English hint in some cases.

MistakeExampleMessageFix
Trailing comma in an object {"a": 1,} Expected double-quoted property name... Remove the trailing comma before this bracket. Delete the last comma
Trailing comma in an array [1, 2, 3,] Unexpected token ']' Delete the last comma
Single quotes {'name': 'Ann'} Expected property name or '}'... must use double quotes Use " for keys and strings
Unquoted key {name: "Ann"} Expected property name or '}'... must be wrapped in double quotes Write "name"
Missing comma {"a": 1 "b": 2} Expected ',' or '}' after property value Add a comma between items
Comment {"a": 1 // note} Expected ',' or '}'... Comments are not allowed in JSON. Remove the comment

How to check your JSON, step by step

  • Paste the JSON into the box labelled JSON input in the JSON Formatter. The Sample button loads a small valid example if you want to see how it works first.
  • Click Validate to check it without changing anything, or Format / Beautify to check it and lay it out with indentation.
  • If it is invalid, read the message under the buttons: it gives the browser's description, the line and column, and for common mistakes a hint such as "Remove the trailing comma before this bracket."
  • Fix the problem in the input and click again. Repeat until the message says Valid JSON.
  • Copy or download the result. Use as input moves the formatted result back into the input box if you want to keep editing it.

Reading the position

A message like "position 8 (line 1, column 9)" tells you where the parser stopped, counted from the start. With a missing comma, as in {"a": 1 "b": 2}, position 8 is the start of "b", because that is where a comma should have appeared. So look just before the position as well as at it.

One mismatched bracket can do the same thing. In {"tags": ["a", "b"} the parser complains at the closing brace, position 18, because it expected the array to end first. Formatting a long document first makes this kind of mistake much easier to see.

Positions are counted from 0, so position 8 is the ninth character, which is why the formatter reports it as column 9. Chrome and Edge give a position or a line and column; Firefox gives a line and column. The formatter turns either into the same line and column, so you can jump to the spot in your editor.

Six more mistakes that break JSON

The table above covers the most common errors, but these come up often too, especially with data copied from documents, spreadsheets or code.

  • Curly quotes. Word processors and chat apps often turn "straight" quotes into “curly” ones. JSON only accepts the straight double quote, so retype the quotes or paste through a plain-text editor.
  • Unescaped backslashes. A Windows path such as "C:\Users\ann" is invalid, because \U is not a valid escape. Write each backslash twice: "C:\\Users\\ann".
  • Line breaks inside a string. A string cannot contain a real line break; use \n for a new line and \t for a tab.
  • JavaScript values. undefined, NaN and Infinity are not JSON. Use null, or a number, or a string.
  • Number formats. Leading zeros such as 007, hexadecimal such as 0xFF, a leading + sign and a trailing decimal point such as 5. are all invalid. Write 7, 255, 5 and 5.0.
  • Truncated data. If a file or API response was cut off, the brackets never close and the parser reports that the input ended too early. Get the full response again rather than adding brackets by hand.

A quick way to debug a large file

The usual culprits are hand-edited config files, JavaScript objects pasted as if they were JSON, and copying from a document that turned straight quotes into curly ones.

If the error position makes no sense, the real problem may be much earlier. An unclosed string, for example, swallows everything up to the next quote mark, so the parser may only complain many lines later. Look upwards from the reported line for a string that is missing its closing quote.

  • Paste the JSON into the formatter and note the line and column in the message.
  • Check that spot and the character just before it.
  • Fix one error and format again: the next error often only appears after the first is gone.
  • If the JSON came from code, fix it there too, or the same error will return.

Worked example: fixing a broken config file

Suppose you paste this into the formatter: {name: 'my-app', "port": 8080, "debug": true, // local only }. It contains three problems: an unquoted key, single quotes and a comment, plus a trailing comma hidden before the comment.

The first message points at name and says keys must be wrapped in double quotes. Change it to "name" and validate again. The next message, in Chrome, reports an unexpected ' token, the single quote; change 'my-app' to "my-app". The third message says comments are not allowed; delete // local only. The last error is the trailing comma after true. Remove it and the message changes to Valid JSON.

This is why it pays to fix one error at a time. The parser stops at the first problem it meets, so it can only ever show one.

What formatting changes, and what it does not

Format / Beautify reads your JSON and writes it out again with 2 spaces, 4 spaces or tabs, whichever you choose in the Indent menu. Minify removes all the spacing to make the file as small as possible. Sort keys A-Z puts the keys of every object in alphabetical order, which makes two versions of a file easier to compare.

Because the formatter rebuilds the data, a few details can change. Numbers are rewritten in their simplest form, so 1.0 becomes 1 and 1e3 becomes 1000. Escapes such as \u00e9 are written out as the character itself, é. If an object has the same key twice, only the last value is kept. And very large whole numbers lose precision: JavaScript numbers are exact only up to 9,007,199,254,740,991, so a long ID like 12345678901234567890 comes back as 12345678901234567000. Keep large IDs as strings if they must stay exact.

Validate never changes anything, so use it when you only need a yes or no.

JSON is not JavaScript

Most of these errors come from the same confusion. JavaScript objects allow single quotes, unquoted keys, trailing commas and comments. JSON allows none of them. The rules are short and fixed by the standard, so a strict error is the parser doing its job.

If a value holds binary data or an encoded string, a tool like the Base64 Encoder can help you see what is inside. Your JSON stays in your browser in the formatter: our network log showed 0 bytes sent during these tests, so it is safe for config files, though you should still be careful where you paste live secrets.

Preventing invalid JSON in the first place

  • Generate JSON with your language's built-in encoder, such as JSON.stringify in JavaScript or json.dumps in Python, instead of joining strings by hand.
  • Use an editor that highlights JSON syntax, so a missing quote stands out as you type.
  • Do not write JSON in a word processor; it changes quotes and dashes.
  • If you need comments in a config file, check whether the program accepts a format that allows them, such as JSON5 or YAML, rather than adding them to strict JSON.

Try the free JSON Formatter

Frequently asked questions

Why does JSON not allow trailing commas?

The JSON standard simply does not include them. A comma must be followed by another value.

Can JSON have comments?

No. Standard JSON has no comment syntax. Some tools accept variants such as JSON5, but a strict parser will reject them.

Why is the error position after my mistake?

The parser reports where it could no longer continue, which is often the character right after the real problem.

Is a single number or string valid JSON?

Yes. Under the current standard, any JSON value can stand on its own, so 42, "hello", true and null are all valid documents.

Why did my long ID number change after formatting?

JavaScript numbers are only exact up to 9,007,199,254,740,991. Larger whole numbers are rounded when the JSON is read. Store such IDs as strings.

Does the formatter fix errors automatically?

No. It shows where the error is and, for common mistakes, what to change, but you make the fix yourself so nothing is changed without you seeing it.

Sources