How to Validate JSON and Read Parser Error Messages Correctly

Learn what JSON validation checks, how to decode "unexpected token at position" errors, why the reported line can mislead, and how to validate in scripts or CI.

Updated September 29, 2026 6 min read

Use the free JSON ValidatorCheck JSON syntax with the exact line and column of any error

An API returns a 400 with "Malformed request body". A deployment fails because a config file will not load. A browser console shows SyntaxError: Expected ',' or '}' after property value in JSON at position 1847. Position 1847 of what, exactly? JSON errors are precise but not always helpful, and the character the parser complains about is often not where the mistake is.

This guide explains what JSON validation does and does not check, how to translate parser messages into a line you can fix, and how to validate JSON automatically in scripts and continuous integration so broken files never reach production.

Two kinds of JSON validation

"Validate JSON" can mean two different things:

  1. Syntax validation asks: is this text well-formed JSON at all? Are the brackets balanced, the strings double-quoted, the commas in the right places? Every parser does this.
  2. Schema validation asks: does this well-formed JSON have the right shape? Is price a number, is email present, are there unexpected fields? That requires a separate description of the expected structure, usually a JSON Schema.

This guide focuses on the first. If you need the second, you can create a starting schema from a sample with the JSON Schema Generator.

What the JSON grammar allows

JSON, as defined in RFC 8259, is stricter than the JavaScript object syntax it resembles:

  • Keys must be strings in double quotes. {name: "x"} and {'name': "x"} are invalid.
  • No trailing commas after the last item in an object or array.
  • No comments of any kind.
  • Values are limited to strings, numbers, true, false, null, objects and arrays. undefined, NaN and Infinity are not allowed.
  • Numbers cannot have leading zeros (007), a leading plus sign, or a bare decimal point (.5).
  • Control characters such as a raw tab or newline must be escaped inside strings (\t, \n).

Formats such as JSONC (used by some editor settings files) and JSON5 relax several of these rules. A file that works in your editor may therefore fail in a strict parser.

Reading error messages

Parsers report the point where they could no longer continue, not the point where you made the mistake. That distinction explains most confusion.

Take this file:

{
  "name": "Desk lamp",
  "price": 49.5
  "stock": 12
}

The missing comma belongs at the end of line 3, but the parser only notices when it meets "stock" on line 4. Node.js 20 reports it like this:

Expected ',' or '}' after property value in JSON at position 43

Newer runtimes add the line and column, and messages differ between browsers and versions, so do not rely on the exact wording. The practical rule: look at the reported position and then at the line just before it.

Some patterns worth recognising:

Symptom in the message Likely cause
Error at the very end of the file An unclosed {, [ or string much earlier
Unexpected token ' Single-quoted strings or keys
Unexpected token } or ] A trailing comma before it
Unexpected token / A comment in the file
Bad control character in string A raw line break or tab inside a string
Unexpected token at position 0 Not JSON at all: an HTML error page, an empty body, or a byte order mark

The last row matters for API work. When response.json() fails at position 0, log the raw response text first. It is very often an HTML error page from a proxy or server.

Validate JSON with our tool

The JSON Validator converts positions into line and column numbers and shows you the neighbourhood of the error.

  1. Open the JSON Validator and paste your JSON into JSON to validate, or click Open file to load a .json file (up to 20 MB). Load sample (has an error) shows how an error is reported.
  2. The result updates as you type. Valid input shows Valid JSON with the size and line count. Invalid input shows Invalid JSON with the line and column, the parser's message, and the surrounding lines with a caret under the failing character.
  3. Click Go to error to jump the cursor straight to that spot in the text area, fix it, and watch the status change.
  4. When the JSON is valid, the tiles summarise the structure: Keys, Max depth, Objects / arrays and Values, broken down into strings, numbers, booleans and nulls. That is a quick sanity check that an export contains what you expected.
  5. Use Format to pretty-print with two-space indentation, Minify to compact it, or Copy to take it elsewhere.

Everything runs in your browser, which matters when the JSON contains customer records or access tokens.

For a walkthrough of fixing each common error, see How to Format JSON and Fix the Most Common Syntax Errors.

Validating JSON in scripts and CI

For files in a repository, automatic checks beat manual ones. This Node.js script validates any number of files and exits with an error code if one fails, which is exactly what a CI job needs:

import { readFileSync } from "node:fs";

for (const file of process.argv.slice(2)) {
  const text = readFileSync(file, "utf8").replace(/^/, "");
  try {
    JSON.parse(text);
    console.log(`ok    ${file}`);
  } catch (err) {
    console.log(`error ${file}: ${err.message}`);
    process.exitCode = 1;
  }
}

Run it as node validate.mjs config/*.json. The replace strips a UTF-8 byte order mark, which some Windows editors add and which JSON.parse otherwise rejects.

Other quick options:

  • jq: jq empty config.json prints nothing for valid JSON and exits with a non-zero status on a syntax error.
  • Python: python -m json.tool config.json > /dev/null does the same with the standard library.
  • Editors: most code editors underline JSON errors as you type. Make sure the file is recognised as JSON, not JSONC, if the target system is strict.

Troubleshooting tips

The file looks perfect but still fails

Look for invisible characters: a byte order mark at the start, a non-breaking space, or "smart" curly quotes pasted from a word processor or chat app. Curly quotes look like " but are different characters. Retype the quotes around the reported position.

It works in one tool and fails in another

One of them is lenient. Browsers' JSON.parse is strict; some configuration loaders accept comments and trailing commas. Validate against the strictest consumer.

Duplicate keys pass validation

The JSON grammar does not forbid duplicate keys, and most parsers keep the last value silently. A validator will call such a file valid, so compare suspicious documents with the JSON Compare tool or review them by eye.

The error is in a huge single-line file

Minified JSON puts everything on line 1, so the column is what counts. Format the file once it is fixed, or use the Go to error button, which works regardless of line length. Then inspect deeply nested data comfortably in the JSON Viewer.

Frequently asked questions

What is the difference between a JSON validator and a JSON linter?

In everyday use they mean the same: a tool that checks syntax. Some linters also enforce style, such as indentation or key order, which is not required for valid JSON.

Why does the error point to the wrong line?

The parser reports where it got stuck, which is usually the token after the real mistake. A missing comma at the end of one line is reported at the start of the next.

Can JSON contain comments?

Not standard JSON. Comments cause a syntax error in strict parsers. Use a separate documentation file, a "_comment" field, or a format that supports comments, such as YAML or JSONC where the consumer allows it.

Is an empty file valid JSON?

No. A JSON text must contain exactly one value. The smallest valid documents are things like {}, [], 0 or null.

Does valid JSON mean my API will accept it?

Not necessarily. Syntax validation only proves the text is well-formed. The API may still reject missing fields or wrong types, which is what schema validation checks.

Try it now: JSON Validator

Loading tool…