How to Validate JSON and Fix the Most Common Errors

Published

"Unexpected token", "Expected property name", "Unexpected end of JSON input": parser errors are terse. This guide explains how to read them and lists the mistakes behind almost every invalid JSON document, with the fix for each.

How to read a JSON error

A parser stops at the first character it cannot accept and reports its position. Two details matter:

  • The position points to where parsing failed, not always where the mistake is. A missing comma at the end of line 4 is reported at the first character of line 5.
  • An error at the very end usually means truncation. Something opened was never closed.

The JSON Validator converts the parser's position into a line and column, highlights the line and says when the input looks truncated.

The eight most common errors

ErrorExampleFix
Trailing comma{"a": 1,}Remove the comma after the last item
Single quotes{'a': 1}Use double quotes
Unquoted key{a: 1}Quote the key: {"a": 1}
Comment{"a": 1 // note}Remove it, or use JSON5
Missing comma{"a": 1 "b": 2}Add a comma between items
Python/JS literal{"a": None}Use null, true or false
Raw line break in a string"line one line two"Use \n
Truncated input{"a": [1, 2Close every bracket and brace

A reliable way to find the problem

  1. Paste the document into the validator and note the line and column.
  2. Look at the reported character and the end of the previous line, where missing commas usually are.
  3. If the error is at the end, count opening and closing brackets; the JSON Viewer or JSON Editor makes nesting easier to see once part of the document parses.
  4. Fix one error, then validate again. Parsers stop at the first problem, so there may be more.

Validating line-delimited JSON

Logs and data exports often use NDJSON (JSON Lines): one complete JSON value per line, with no commas between lines. A normal JSON parser rejects the whole file. Convert Ferry's JSON tools detect this format and check each line separately, reporting the line number of any bad record.

{"event":"login","user":1}
{"event":"logout","user":1}

Valid JSON versus valid data

Syntax validation proves that a document is JSON. It does not prove that it has the fields your application expects. For that, describe the expected structure with JSON Schema and validate against it in your code. The JSON to JSON Schema generator gives you a starting schema from a sample.

Preventing invalid JSON

  • Generate JSON with a serializer such as JSON.stringify or json.dumps instead of building strings by hand.
  • Escape user text with a JSON library, never with string concatenation.
  • Validate in CI for JSON files kept in a repository.

Frequently asked questions

Why does my JSON work in JavaScript but fail in the validator?

A JavaScript object literal allows unquoted keys, single quotes, comments and trailing commas. JSON does not.

What does "Unexpected end of JSON input" mean?

The document ended before every object, array or string was closed. Often only part of a response was copied.

Why is the error on a line that looks fine?

The parser reports where it noticed the problem. Check the end of the previous line for a missing comma or quote.

Related guides