Web
Find the error in invalid JSON, and fix it
The mistakes that make JSON invalid, how to read the line and column a parser reports, and two valid-but-wrong cases: duplicate keys and big numbers.
JSON is strict on purpose. A config file, an API response or a log line that looks fine to you can still be rejected, and the error message often points one character past the real mistake. This guide covers the mistakes I see most, how to read what a parser tells you, and two cases where the JSON is valid but the data still isn't what you meant.
I'll use my JSON Formatter and Validator for the examples. It runs in your browser, parses what you paste as data and never runs it as code, so it's safe for a payload you don't fully trust.
#The rules JSON doesn't bend
Most "invalid JSON" errors come from writing something that JavaScript or YAML would accept. The JSON standard (RFC 8259) is narrower:
- Strings and keys use double quotes.
'name'andnameare not keys."name"is. - No trailing commas.
[1, 2, 3,]and{"a": 1,}are both invalid. - No comments. Neither
//nor/* */exists in JSON. If your tool accepts them, it's reading a looser format. - Only these values: strings, numbers,
true,false,null, objects and arrays.undefined,NaNandInfinityare not JSON. - Numbers are plain decimals. No leading zeros (
007), no hex (0x1F), no leading+, and no bare decimal point (.5or5.). - Control characters inside strings must be escaped. A raw line break or tab inside quotes is invalid. Write
\nor\tinstead. - One value per document. Two objects pasted one after the other is two documents, and a parser will stop at the start of the second.
If you only remember one thing: copy-pasted JavaScript object literals are the most common source of bad JSON, because they allow single quotes, unquoted keys, comments and trailing commas.
#Reading the line and column
When the input doesn't parse, the formatter shows the parser's reason and, when it can work one out, a line and column, for example "Invalid JSON. Expected double-quoted property name. Line 4, column 3."
That position is where the parser gave up, which isn't always where you went wrong. Three patterns cover most cases:
- A trailing comma is reported at the closing
}or]after it. Look one token back. - A missing comma between two items is reported at the start of the second item. The fix is at the end of the line before.
- An unclosed string or bracket is often reported at the very end of the input, because the parser only learns something is missing when it runs out of text. Work backwards from the end and check that every
",{and[has its pair.
The exact wording comes from your browser's own JSON parser, so it can differ a little between browsers. The position is the part to trust.
#A quick way to narrow it down
For a big document where the message isn't enough:
- Format what you have. Indentation makes a missing bracket visible as a block that never closes.
- Cut the document in half and check each half on its own, wrapped in
[and]if needed. The half that fails holds the error. - Repeat until the bad part is a few lines long.
The formatter takes up to 1 MB and up to 100 levels of nesting. Anything bigger needs a desktop editor or a streaming parser, and the page tells you which limit you hit.
#Invisible characters
Two invisible problems come up again and again.
The first is a byte order mark, a hidden character some Windows tools put at the start of a file. Many parsers reject it. The formatter strips a leading byte order mark before parsing and tells you it did, so you know to save the file without one before another program reads it.
The second is a curly quote. Text that passed through a word processor or a chat app can have “ and ” in place of ". They look almost the same and are not valid. If a line looks right and still fails, retype its quotes.
#Valid JSON that still isn't what you meant
Passing the parser doesn't mean the data is right. Two cases are worth knowing.
Duplicate keys. {"port": 80, "port": 8080} is accepted by most parsers, and they quietly keep only the last value. The standard says keys should be unique and leaves the rest to each program, so the same file can behave differently in two places. The formatter checks for repeated keys in the same object and warns you with their names, so you can decide which value you actually meant.
Big numbers. JSON allows a number of any size, but most programs read numbers as 64-bit floating point, which holds whole numbers exactly only up to 9,007,199,254,740,991. An ID like 9007199254740993 can come back one off, and 1e400 turns into infinity. When the formatter formats or minifies, it writes every number back exactly as you typed it, so tidying a file never changes a value. The program that reads the file later may still round it, so IDs that large are safer as strings.
#Format, minify or sort
Once the input is valid you can:
- Format it with 2 or 4 spaces of indentation, for reading and for diffs.
- Minify it to one line, for sending or storing.
- Sort object keys, recursively. Array order and every value stay the same. Sorted keys make two versions of the same file much easier to compare.
Then copy the result or download it as formatted.json. The summary under the output shows the nesting depth, the key count and the output size.
#A short checklist
- Double quotes on every key and string.
- No trailing commas, no comments.
- No
undefined,NaN,Infinity, hex or leading zeros. - Escaped line breaks and tabs inside strings.
- Look one token before the reported position.
- Check for duplicate keys and for IDs above 9,007,199,254,740,991.
If your JSON is a server log, my guide on counting real readers from a server log and the Access Log Reader pick up from there. The rest of my free tools are on the tools page.