Free online tool

Upload a file

Drop your file hereTap to choose a file

or click to browse, or paste from the clipboard

← Back to Blog

Invalid JSON: How to Find and Fix the Most Common Errors

Invalid JSON is text that breaks one of JSON's strict syntax rules, so a parser refuses to read it. The usual causes are a trailing comma, single quotes, an unquoted key, a comment, a value such as NaN or undefined, or an invisible byte order mark (BOM), while an Unexpected token < error usually means the server sent an HTML page instead of JSON. Paste the text into the JSON Formatter & Validator to see the line and column of the first problem, then use the fixes below. It runs in your browser, so nothing you paste is uploaded.

How to find invalid JSON with Zutilium's JSON validator

  1. Open the JSON Formatter & Validator and paste your text, up to 524,288 characters, into the JSON input box.
  2. Select Validate. Correct syntax shows "Valid JSON syntax." in the status line below the boxes.
  3. Otherwise the status line names the first problem and its position, such as "Expected a comma or } at line 3, column 3." Fix it and select Validate again until the text passes.
  4. For a readable copy, choose 2 spaces or 4 spaces under Indentation and select Format. Select Minify for a single line instead. Set the indentation first, because changing it clears the result.
  5. Select Copy result, or Download result to save the output as document.json.
  6. After Format or Minify, documents of up to 1,200 tokens also get Explore JSON tree below the result, where you can expand and collapse each object and array.

The output should differ from your input only in whitespace: long IDs, decimals such as 19.90, exponents, key order and repeated keys come through exactly as pasted, because the tool never converts the text to JavaScript values. It never repairs anything either, so every fix is one you made.

Worked example: one broken config, six fixes

Suppose this config was copied out of a JavaScript file and edited by hand. It contains six errors:

{
  name: 'checkout-api',
  "port": 8080, // default
  "timeout_ms": 2500,
  "ratio": NaN,
  "regions": ["eu-west", "us-east",],
}

Select Validate, fix what the message points to, and repeat:

Run Validator message Fix
1 Invalid JSON token at line 2, column 3. Quote the key: "name"
2 Invalid JSON token at line 2, column 11. Use double quotes: "checkout-api"
3 Invalid JSON token at line 3, column 17. Delete // default
4 Invalid JSON token at line 5, column 12. Change NaN to null
5 Expected a JSON value at line 6, column 36. Delete the comma after "us-east"
6 Expected a quoted property name at line 7, column 1. Delete the comma at the end of line 6
7 Valid JSON syntax. Done

Runs 5 and 6 show how to read a position. A parser reports where it stopped, not where the mistake began, so a stray comma is reported at the bracket or brace after it. When the column lands on a closing bracket or on the first key of a line, look just before it, including the end of the previous line.

Errors inside a string are reported at its opening quote as an invalid token. A raw line break, a tab, a bad backslash and a missing closing quote all look like that, so read along the string from that point.

Common invalid JSON errors and exact fixes

JSON, defined in RFC 8259, is stricter than the JavaScript and Python it resembles. These are the usual mistakes, each with the smallest fix:

Mistake Invalid Valid
Trailing comma [1, 2, 3,] [1, 2, 3]
Missing comma "a": 1 "b": 2 "a": 1, "b": 2
Single quotes {'id': 'A1'} {"id": "A1"}
Unquoted key {id: 1} {"id": 1}
Comment "port": 80 // web "port": 80
Not a JSON value NaN, Infinity, undefined null or a string
Python literals True, False, None true, false, null
Leading zero "zip": 02134 "zip": "02134"
Bare decimal point .5, 5. 0.5, 5.0
Single backslash "C:\Users" "C:\\Users"
Two documents {"a": 1}{"a": 2} [{"a": 1}, {"a": 2}]

For a leading zero, turn codes such as ZIP or account numbers into strings so the zero survives, and drop the zero from real quantities.

Comments, trailing commas, JSON5 and JSONC

Some JSON-like files allow comments and trailing commas. VS Code reads settings.json as JSON with Comments, which accepts // and /* */ and tolerates trailing commas with a warning, and JSON5 also adds single quotes, unquoted keys, hexadecimal numbers, NaN and Infinity. Strict parsers, including this validator, reject all of them. If data read by a strict parser needs notes, use a field such as "_comment" or a format with real comments, as the JSON to YAML guide explains.

NaN, Infinity, undefined and printed objects

These usually come from a lenient serializer. According to the Python json documentation, json.dumps writes NaN and Infinity by default; pass allow_nan=False to raise an error instead, and convert such values to null first. JavaScript's JSON.stringify turns NaN and Infinity into null and drops undefined properties, which keeps the output valid but changes the data.

Printed objects are the other source. Python's print() shows {'active': True, 'manager': None} and Node's console.log shows { name: 'Ada' }. Neither is JSON, so copy the output of json.dumps() or JSON.stringify() instead.

Invisible characters: BOM, smart quotes and non-breaking spaces

If the error is at line 1, column 1 and nothing looks wrong, the text probably starts with a byte order mark (U+FEFF). RFC 8259 forbids adding one to JSON sent over a network and allows, but does not require, parsers to ignore it. JSON.parse rejects it, and Python's json.loads raises Unexpected UTF-8 BOM (decode using utf-8-sig). Windows PowerShell 5.1 writes a BOM with -Encoding UTF8 and PowerShell 7 does not, according to Microsoft's PowerShell encoding notes. Save the file as UTF-8 without a BOM, or strip it when reading:

import json

with open("data.json", encoding="utf-8-sig") as f:  # utf-8-sig skips a leading BOM
    data = json.load(f)
const data = JSON.parse(text.replace(/^\uFEFF/, ""));

Smart quotes from a word processor and non-breaking spaces from a web page look like the real characters, but the validator reports each one as an invalid token at its exact column. Retype them there.

Line breaks, tabs and backslashes in strings

Inside a string, double quotes, backslashes and control characters such as line breaks and tabs must be escaped as \", \\, \n and \t, so a Windows path needs doubled backslashes. \' is not a valid escape: an apostrophe inside double quotes needs none.

Unexpected token in JSON: what the messages mean

An unexpected token is the first character a parser could not use. The wording of the error varies by browser and runtime:

Message Seen in Usual cause
Unexpected token '<', "<!DOCTYPE "... is not valid JSON Chrome, Edge, Node.js An HTML page came back
Unexpected token < in JSON at position 0 Older Chrome and Node.js An HTML page came back
JSON.parse: unexpected character at line 1 column 1 of the JSON data Firefox The text does not start with JSON
JSON Parse error: Unrecognized token '<' Safari An HTML page came back
Unexpected end of JSON input Chrome, Edge, Node.js Empty or cut-off text
"undefined" is not valid JSON Chrome, Edge, Node.js You parsed an unset variable
"[object Object]" is not valid JSON Chrome, Edge, Node.js The data was already parsed
Expecting value: line 1 column 1 (char 0) Python Empty or non-JSON response

A < usually means the request worked but the server answered with HTML: a 404 or 500 page, a login redirect or a proxy error. DataTables' "Invalid JSON response" warning and the parsererror status from jQuery's $.ajax often have the same cause. Read the body with await response.text() instead of response.json(), check the status code, and fix the URL, the authentication or the server error. A body that starts with <?xml is XML, which the XML Formatter reads. For any other message, copy the exact text your code parsed and validate it.

Pretty-print versus minify in a JSON formatter

Pretty-printing adds line breaks and indentation for people; minifying strips every space and line break outside strings. The data is identical: the fixed config above is 98 characters minified, 132 with 2 spaces and 152 with 4.

Keep formatted copies for files that people read and review. Use minified copies where one line is required or space matters: request bodies, environment variables, command-line arguments and JSON Lines logs. To compare two versions, use JSON Diff, which ignores formatting and key order; the guide to comparing two JSON files explains why that matters.

Why validators and formatters disagree

Checkers are not equally strict. Give the same inputs to JSON.parse in Node.js, python3 -m json.tool and jq empty, and the verdicts differ:

Input Zutilium JSON.parse json.tool jq 1.7.1
NaN, Infinity Error Error Accepted Accepted
Leading zero 01 Error Error Error Read as 1
BOM at the start Error Error Error Skipped
Repeated key Both kept Last wins Last wins Last wins

So a Python service can write NaN, read it back without complaint, and break its JavaScript client.

Big numbers lose digits

The JSON grammar sets no size limit on numbers, but JavaScript stores every number as a 64-bit double, which holds integers safely only up to 2⁵³ − 1 (9,007,199,254,740,991, Number.MAX_SAFE_INTEGER). From 2⁵³ upward, doubles are 2 apart, so 9007199254740993 becomes 9007199254740992, and RFC 8259 names the same range as the one implementations agree on. Two common reformatting routes change this record:

{"order_id":9007199254740993,"total":19.90,"tax_rate":1e-1,"sizes":{"10":"L","2":"S"},"status":"paid","status":"refunded"}
Input JSON.parse, then JSON.stringify python3 -m json.tool
9007199254740993 9007199254740992 Unchanged
19.90 19.9 19.9
1e-1 0.1 0.1
Keys "10", "2" Reordered: "2", "10" Unchanged
Two "status" keys Only "refunded" Only "refunded"

The JSON Formatter & Validator returns all of it unchanged. JavaScript also lists integer-like keys first, in ascending order, because that is how its objects order properties.

Send IDs as strings where you can, as MDN's JSON.parse reference suggests for large numbers. Otherwise read the original digits: in Chrome 114+, Firefox 135+, Safari 18.4+ and Node.js 21+, a reviver receives them as context.source.

const data = JSON.parse(text, (key, value, context) =>
  key === "order_id" ? BigInt(context.source) : value);

Python keeps long integers exact; add parse_float=decimal.Decimal to keep decimals such as 19.90 exact too. Spreadsheets can round long IDs too; the guide to long order IDs in Excel shows how to keep them.

Duplicate keys

Repeated keys pass a syntax check, but RFC 8259 says names should be unique and warns that receiving software behaves unpredictably otherwise. JSON.parse and Python keep the last value, so "status" in the record above ends up as "refunded". The validator keeps both copies without a warning, so scan the formatted output, or let JSON to YAML count the repeats and name the first few as it converts.

Troubleshooting

It passes here, but your app still rejects it

Validate checks syntax, not structure, so an app that expects certain fields needs a schema check, which this tool does not do. If the app reads a file, that file may still hold a BOM that most editors hide; save it as UTF-8 without one.

The error is at line 1, column 1

Something before the first { or [ is not JSON: an HTML page, a BOM, a sentence such as "Here is the JSON:", a Markdown code fence from AI output or a JSONP wrapper like callback(. Delete everything outside the outer brackets and validate again.

Input exceeds the 524,288 character limit

The validator handles 524,288 characters and 128 levels of nesting, and Format stops above 4,194,304 output characters, in which case use 2 spaces or Minify. For larger files, python3 -m json.tool big.json > /dev/null prints the first error with its line and column, though it accepts NaN and Infinity.

Frequently asked questions

What does invalid JSON mean?

The text breaks the JSON grammar, so a strict parser stops at the first character it cannot accept. A validator shows where that happened, which is at the mistake or just after it.

How do I fix "Unexpected token < in JSON at position 0"?

Your code received HTML, usually an error or login page, and tried to parse it as JSON. Log the status code and the body from response.text(), then fix the URL, the authentication or the server error behind that page.

Can JSON have comments?

No. The grammar has no comment syntax, so // and /* */ make a document invalid. JSON with Comments and JSON5 allow them, but only tools that read those formats accept the file, so keep notes in a field such as "_comment" instead.

Is a trailing comma allowed in JSON?

No. Commas only separate items, so [1, 2, 3,] and {"a": 1,} are invalid even though JavaScript, Python and JSON5 accept that style. Delete the comma before the closing bracket or brace.

Are duplicate keys valid JSON?

They pass a syntax check because the grammar allows them, but RFC 8259 says names should be unique. JavaScript and Python keep only the last value and other parsers may reject the object, so remove or rename the repeat.