Skip to content
← SyntaxBench reference notes

What an Invalid JSON Syntax Error Means and How to Fix It

Contents
AI-generated illustration; not taken on location · Conceptual diagram of valid JSON object syntax
AI-generated illustration; not taken on location · A generic diagram explaining valid JSON object structureAI-generated illustration

An invalid JSON syntax error means a parser reached text that does not follow JSON syntax. When an application or service sends data that claims to be JSON, the receiving parser checks whether every character, punctuation mark, and structural rule matches the JSON specification. If the parser finds something it cannot interpret, it stops and reports a syntax error instead of processing the data.

These errors commonly appear when developers call APIs, edit configuration files, or move data between systems. The problem is not always in the visible portion of the file; a missing quote, an extra comma, or an unexpected HTML page can break the entire payload. Because parsers typically read documents from top to bottom, the reported error location may appear near the end even though the actual mistake happened much earlier.

This guide explains what these errors mean, shows frequent causes and examples, and describes practical ways to identify and fix them using validator tools. Readers will learn how to read parser messages, recognize common malformed patterns, and decide what to verify when a payload fails to parse.

Anatomy of JSON Syntax Errors

A JSON syntax error means a parser has reached text that does not follow JSON grammar. When this happens, parsing stops and the program reports a problem instead of producing usable data.

JSON is stricter than many data formats. Objects must be enclosed in curly braces, arrays in square brackets, property names in double quotation marks, and string values in double quotation marks. Values must be valid JSON types: strings, numbers, objects, arrays, booleans, or null. Commas separate items, but a comma after the final item in an object or array is not permitted.

When the parser encounters text that breaks these rules, it usually emits an error that includes a description such as "unexpected token," together with a line number and column position. That location marks where the parser stopped. It does not prove that the mistake was introduced at exactly that spot.

For example, if a JSON object is missing its closing brace, the parser may keep reading through later text until it reaches another object’s opening brace or the end of the document. The error then appears at that later point, even though the actual omission occurred much earlier.

This behavior explains why parsers often report errors near the end of a document rather than at the true origin of the mistake. The parser cannot know what the author intended; it can only report where the text first became impossible to continue under JSON grammar.

Some environments return HTML instead of JSON. If a server sends an error page or a login form when the code expected data, the parser encounters characters such as < or > where JSON expects a value. This commonly produces an "unexpected token <" message. That message is a signal to check the response source and confirm whether the server actually returned JSON.

A missing comma can have a similar cascading effect. Without the comma between two items, the parser may treat the following text as part of the previous value and fail only when it reaches an unexpected character later.

Understanding this distinction helps avoid editing the wrong part of a file. Treat the parser’s reported line and column as a clue, then inspect the surrounding structure to locate the real violation.

Common Causes and Examples

Several recurring mistakes produce invalid JSON syntax errors. Each example below shows a malformed pattern and why a parser rejects it.

Trailing commas

JSON does not permit a comma after the final item in an object or array. A parser reading this example stops after the last value because it expects either another item or the closing bracket, not a comma:

{
  "name": "example",
  "version": 1,
}

The comma after 1 is invalid. Removing it allows the parser to reach the closing brace normally.

Unquoted property names

Property names in JSON must be enclosed in double quotes. Unquoted names are valid in some JavaScript object syntax, but not in JSON:

{
  name: "example",
  "version": 1
}

A parser encounters name without quotes and reports an unexpected token because JSON grammar requires a quoted string at that position.

Single quotes

JSON strings must use double quotes. Single quotes are not string delimiters in JSON:

{
  'name': 'example'
}

The parser sees ' where it expects a valid JSON value or structure, triggering an unexpected token error. Replacing single quotes with double quotes fixes this pattern.

Invalid numbers

JSON numbers follow specific rules. Leading zeros before the decimal point are not allowed in standard JSON, and numbers cannot contain stray characters:

{
  "value": 01.5
}

Depending on the parser, this may be rejected because the number does not conform to accepted numeric grammar.

HTML instead of JSON

If a request fails and the server returns an HTML error page or login screen, the response may begin with <html> or <!DOCTYPE html>. A JSON parser expects {, [, or a JSON literal, so it reports an unexpected token <. This is not a mistake inside the JSON data itself; it indicates the response was not JSON at all.

Other structural mistakes

Missing closing braces, mismatched brackets, unescaped control characters inside strings, and duplicate keys can also cause parse failures. Duplicate keys are handled differently by parsers, but structural gaps usually produce clear syntax errors.

When an error message mentions an unexpected token, readers should inspect the character at the reported position and then look backward for missing quotes, braces, brackets, or commas.

Identifying and Fixing Errors with Tools

Validators and formatters help locate syntax problems by parsing the full document and reporting where grammar breaks down. A validator checks whether the text conforms to JSON rules, while a formatter rewrites valid JSON with consistent indentation and spacing. These tools are especially useful for large payloads, where scrolling through raw text makes mistakes difficult to see.

Using a validator

Paste the suspect JSON into a validator and run the check. If the document is invalid, the tool usually reports an error message, line number, and column position. Readers can use that position as a starting point, then inspect nearby characters for missing quotes, commas, braces, or brackets.

For large files, validators can narrow the search significantly. Instead of reviewing hundreds of lines manually, the reader goes to the reported location and checks the surrounding structure. If the message points to the end of the document, the true problem may be an earlier missing closing brace or bracket.

Using a formatter

A formatter can reveal structural issues by attempting to reindent the document. If formatting fails, the tool often reports the same kind of parse error as a validator. When formatting succeeds, the indented output makes mismatched braces and brackets easier to spot visually.

Diagnosing HTML responses

When a validator reports an unexpected token < at the beginning of a response, readers should check the response source directly. If the text begins with <html>, <!DOCTYPE, or contains typical HTML tags, the server returned an HTML page rather than JSON. This can happen after authentication failures, server errors, or incorrect request URLs. The fix usually involves correcting the request, authentication, or endpoint rather than editing the JSON syntax.

Practical workflow

  1. Copy the full response or file into a validator.
  2. Note the reported error message and position.
  3. Inspect the character at that position and the preceding structure.
  4. Look backward for missing closing braces, brackets, quotes, or commas.
  5. Correct one issue at a time and revalidate.
  6. If the error is an unexpected token <, inspect the full response for HTML before assuming the JSON itself is malformed.

Repeated validation after each correction ensures that fixing one mistake does not hide another later in the document.