JSON
Jul 17, 2026
6 min read

Common JSON Errors and Fixes: A Developer's Troubleshooting Guide

Floating neon JSON curly braces and bracket symbols in a dark digital space with red error marker highlights and blue volumetric lighting.

JSON is the backbone of modern web communication. REST APIs return it, configuration files store it, and browsers consume it constantly. But JSON is also strict: a single misplaced comma, an unclosed quote, or a capitalized True instead of true is enough to make a parser refuse the entire document. According to Stack Overflow's annual surveys, JSON-related questions appear consistently among the most-visited debugging threads. This guide walks through the six most common JSON parsing errors developers encounter in practice. Each error includes the exact message you are likely to see, the root cause, a broken and corrected example, and the fix. The article also shows how an online validation tool, the txtnode JSON Formatter, can pinpoint these errors instantly so you spend less time staring at error logs and more time shipping code.

Error: Unexpected token X in JSON at position Y#

This is by far the most frequent JSON parsing error. You have probably seen it in your browser console, inside a fetch() error handler, or in a Node.js log. The message tells you exactly where the parser stopped and what character it found instead of a valid JSON token.

What it means. The parser was expecting one of the valid JSON value types (a string, number, object, array, boolean, or null) and encountered something else at the given character position.

Common causes.

  • A trailing comma after the last element in an object or array.
  • A literal True, False, or None (Python-style) instead of the JSON-lowercase true, false, or null.
  • Single quotes around keys or string values instead of double quotes.
  • A stray character such as an unescaped control character or an extra closing bracket.

Broken example:

{'key': True, 'count': None}

Corrected version:

{"key": true, "count": null}

How to fix. The error message includes a character position number. Count (or copy) to that position in your JSON document and inspect the surrounding characters. In practice, it is faster to paste the JSON into a validator that highlights errors visually. The txtnode JSON Formatter accepts raw JSON input and displays the exact line and character where parsing breaks, which removes the guesswork of manually counting positions.

Error: Trailing comma after the last value#

JavaScript has allowed trailing commas in objects and arrays since ECMAScript 5. Many developers carry this habit into JSON and are surprised when the parser rejects it. JSON is not JavaScript.

What the spec says. RFC 8259, Section 2, defines the syntax of a JSON object: members are separated by commas, and there is no provision for a comma after the final member. The same applies to arrays.

Broken example:

{
  "name": "Alice",
  "role": "developer",
}
[1, 2, 3,]

Corrected versions:

{
  "name": "Alice",
  "role": "developer"
}
[1, 2, 3]

How to fix. Remove the comma after the last item. If you are editing a large file, use a JSON formatter that strips trailing commas automatically. The txtnode JSON Formatter removes them as part of its formatting pass, so the output is spec-compliant without manual line-by-line scanning.

Prevention tip. Configure your linter. For ESLint, enable the comma-dangle rule set to "never" for JSON files. Most IDEs can also be set to strip trailing commas automatically when saving JSON documents.

Error: Unterminated string or unexpected newline inside string#

JSON strings are delimited by double quotes and must be contained on a single logical line. A raw newline character inside a string value causes a parse failure.

What the spec says. Per RFC 8259, a string is a sequence of Unicode code points wrapped in quotation marks. Control characters, including carriage return and line feed, must be escaped as \r and \n. A literal line break is not allowed.

Broken example:

{
  "description": "Hello
World"
}

Corrected version:

{
  "description": "Hello\nWorld"
}

How to fix. Locate the line break inside the string value and replace it with the two-character escape sequence \n. If the string came from multiline text (for example, pasted from a rich-text editor), you can also use an online formatter to detect and escape these characters. The txtnode JSON Formatter flags unterminated strings with a clear error message and shows exactly where the unexpected newline occurs.

Error: Duplicate keys in a JSON object#

This error is often silent. Many parsers, including JSON.parse() in JavaScript, do not throw an exception when they encounter duplicate keys. Instead they silently keep the last value and discard the earlier one. The result is data loss that can be extremely difficult to trace.

What the spec says. RFC 8259, Section 4, says that the names within an object should be unique. It recommends that parsers either report duplicate names as errors or ensure that there is no ambiguity in how they handle them. In practice, most parsers use the last occurrence.

Broken example:

{
  "user": "alice",
  "role": "admin",
  "user": "bob"
}

In this document "user" resolves to "bob" in most environments. The first mapping is silently discarded.

How to fix. Review your JSON manually for repeated keys, or use a tool that explicitly reports duplicates. The txtnode JSON Formatter highlights duplicate keys as it formats your document, so you can see at a glance which names appear more than once.

Prevention tip. If you generate JSON programmatically, build the object structure carefully. In Python, use json.dumps() with check_circular=False as a diagnostic step. In JavaScript, constructing objects via Object.fromEntries() with duplicate keys will overwrite silently, not error. Consider using a schema validator like AJV in your CI pipeline to catch structural issues before they reach production.

Error: Using JavaScript or Python literals instead of JSON values#

A JSON boolean value is true (lowercase). A JSON null value is null (lowercase). Python developers inadvertently write True, False, or None. JavaScript developers may include undefined or NaN. The JSON parser does not recognize any of these because they are not part of the JSON grammar.

What the spec says. RFC 8259 defines exactly three literal names: null, true, and false. No capitalization or spelling variation is accepted.

Broken example:

{
  "active": True,
  "value": None,
  "score": undefined
}

Corrected version:

{
  "active": true,
  "value": null,
  "score": null
}

How to fix. Search your JSON for uppercase keywords and replace them with the lowercase JSON equivalents. Replace undefined with either null (if the value is intentionally absent) or omit the key entirely.

Prevention tip. When serializing data from Python, always use json.dumps() instead of manual string formatting. Python's json module correctly serializes True to true, False to false, and None to null. The same applies to JSON.stringify() in JavaScript, which correctly handles null but will omit undefined values from objects.

Error: Invalid number formats (leading zeros, octal, hexadecimal)#

JSON numbers follow a strict syntax. A leading zero is only allowed when the number is zero itself (0) or when it is followed by a decimal point (0.5). Numbers like 012 or 007 are invalid. Octal and hexadecimal notation are not part of the JSON grammar.

What the spec says. RFC 8259, Section 6, defines the number production: an optional minus sign, an integer part, an optional fraction part, and an optional exponent part. The integer part cannot have leading zeros unless the integer part itself is exactly 0.

Broken example:

{
  "errorCode": 0123,
  "hexValue": 0xFF,
  "octalValue": 0o77
}

Corrected version:

{
  "errorCode": 123,
  "hexValue": 255,
  "octalValue": 63
}

How to fix. Strip all leading zeros from integers and convert hexadecimal and octal literals to their decimal equivalents. Use decimal notation exclusively.

Prevention tip. In JavaScript, JSON.stringify() serializes numbers correctly and will not produce leading zeros. In Python, integers parsed from strings like "0123" via int("0123") produce 123 correctly, but if you construct numbers with manual string formatting, you can accidentally introduce leading zeros. Always verify number values after automated transformations.

How to prevent JSON errors in your workflow#

Knowing how to fix each error is useful. Preventing errors before they reach production is better.

Use a linter. Configure ESLint with the json plugin to catch syntax errors and formatting violations in your editor as you type. The comma-dangle rule, in particular, prevents trailing comma mistakes if set to "never" for JSON files.

Validate in CI. Add a validation step to your continuous integration pipeline. For schema validation, use libraries like AJV (Another JSON Schema Validator). For simple syntax checking, the built-in JSON.parse() test in Node.js or json.loads() in Python is enough for a pre-commit hook.

Keep a validator in your bookmarks. When you are in the middle of debugging and a JSON payload keeps failing, opening a dedicated tool is faster than scanning the file by eye. The txtnode JSON Formatter accepts pasted JSON or uploaded content and returns a formatted, validated, and highlighted result in seconds. It catches trailing commas, unterminated strings, duplicate keys, and unexpected tokens automatically.

JSON syntax is strict but predictable. The six errors covered here represent the vast majority of parsing failures developers encounter in daily work. Once you recognize the pattern behind each error message, the fix becomes routine. Keep a linter running in your editor, validate before commit, and when in doubt, paste the payload into a formatter to see exactly where the problem is.

Information last checked on 2026-07-17.