Tools

Common JSON Errors and How to Fix Them - Developer Guide

DesiUtils Teamยท12 April 2026ยท8 min read
This article is for informational purposes only and does not constitute financial, legal, or tax advice. Consult a qualified professional for your specific situation.

JSON looks simple until it breaks. You paste a payload into your code, hit run, and get Unexpected token or SyntaxError: JSON.parse. The error message points at a character position, but that rarely tells you what is actually wrong.

Most JSON errors come from the same handful of mistakes. This guide covers the ones developers hit most often, explains why they happen, and shows you how to fix them.

{ }
JSON FormatterPaste your JSON to find and fix errors instantly โ†’

1. Trailing Commas

This is the single most common JSON error. In JavaScript, you can write {"name": "test", "active": true,} and it works fine. In JSON, that trailing comma after the last value is invalid.

The fix is simple: remove the comma after the last property in every object and the last item in every array. If you work in a codebase that uses Prettier or ESLint with trailing comma rules, this will catch you every time you copy data from your code into a JSON file.

2. Single Quotes Instead of Double Quotes

JSON requires double quotes for all keys and string values. No exceptions. {'name': 'test'} is valid JavaScript but invalid JSON. So is `name`: `test` with backticks.

This error often shows up when developers hand-write JSON or copy Python dictionary syntax (which uses single quotes) into a JSON context. The fix is a global find-and-replace of single quotes with double quotes, though you need to be careful with values that contain apostrophes.

3. Unescaped Special Characters in Strings

JSON strings cannot contain raw newlines, tabs, or certain control characters. If you paste multi-line text into a JSON string value, the parser will break at the first line break.

Characters that must be escaped inside JSON strings:

  • \" - double quote
  • \\ - backslash
  • \n - newline
  • \t - tab
  • \r - carriage return
  • \uXXXX - Unicode characters

The most common trigger is copying a paragraph of text (like an address or description) directly into a JSON field without escaping the line breaks.

4. Comments in JSON

Standard JSON does not support comments of any kind. Neither // single-line comments nor /* */ block comments are allowed.

This confuses developers because many tools use JSON-like formats that do allow comments. VS Code settings (settings.json), TypeScript configuration (tsconfig.json), and ESLint configs use JSONC (JSON with Comments). But if you feed a JSONC file to a strict JSON parser like JSON.parse(), it will fail.

The fix: strip all comments before using the data as standard JSON. Most code editors can convert JSONC to JSON automatically.

5. Unquoted or Incorrectly Quoted Keys

Every key in a JSON object must be a double-quoted string. {name: "test"} is valid in JavaScript but not in JSON. Similarly, keys wrapped in single quotes will fail.

This happens frequently when converting JavaScript objects to JSON manually instead of using JSON.stringify(). The safest approach is to always use JSON.stringify(yourObject, null, 2) to produce valid, formatted JSON from any JavaScript value.

6. Numbers with Leading Zeros

007 is not a valid JSON number. Neither is 01 or 00123. JSON numbers must not have leading zeros unless the number is exactly 0 or a decimal like 0.5.

This catches people working with IDs, pin codes, or codes that happen to start with zero. If the leading zero is meaningful (like a postal code), store it as a string: "007" instead of 007.

7. BOM or Invisible Characters at the Start

Sometimes JSON looks perfectly valid when you read it, but JSON.parse() still fails at position 0. This often means the string starts with a Byte Order Mark (BOM) or other invisible Unicode character.

BOMs are common when files are saved from Windows editors or when API responses pass through certain proxies. The fix is to strip the BOM before parsing: text.replace(/^\uFEFF/, '') in JavaScript.

8. API Returning HTML Instead of JSON

You call an API expecting JSON, but get Unexpected token <. That < is the start of an HTML tag - the server returned an HTML error page (like a 404 or 500 page) instead of a JSON response.

Before parsing, always check the Content-Type header of the response. It should be application/json. If it is text/html, the server did not return what you expected. Check the URL, authentication, and whether the endpoint is actually up.

JSON Formatter vs JSON Validator

These terms are often used interchangeably, but they do different things:

  • Formatter (or beautifier/pretty-printer) - takes valid JSON and adds indentation and line breaks to make it readable. Does not change the data itself.
  • Validator - checks whether the input conforms to the JSON specification. Reports the exact location of syntax errors.
  • Minifier - the opposite of formatting. Strips all unnecessary whitespace to produce the smallest possible output for transmission or storage.

A good JSON tool does all three. Paste your data, and it tells you whether it is valid, shows you where the errors are, and lets you switch between formatted and minified output.

{ }
JSON FormatterFormat, validate, and minify JSON in one step โ†’

A Practical Debugging Workflow

When you hit a JSON parse error, here is a reliable way to find the problem:

  • Step 1: Paste the raw JSON into a formatter/validator tool. The tool will point to the exact line and character where parsing failed.
  • Step 2: Look at the characters immediately before the error position. The most common culprits are trailing commas, single quotes, unescaped newlines, and missing quotes on keys.
  • Step 3: If the JSON looks correct visually, check for invisible characters. Copy the first few characters into a hex viewer or use JSON.parse(text.trim().replace(/^\uFEFF/, '')) to strip BOM and whitespace.
  • Step 4: If the data comes from an API, log the raw response before parsing. Confirm the Content-Type is application/json and that the body is not an HTML error page.
๐Ÿ”
Base64 Encoder/DecoderDecode Base64-encoded API payloads โ†’
๐Ÿ”
Text Diff / CompareCompare two JSON responses side by side โ†’

Related Posts

Frequently Asked Questions

Why does JSON not allow trailing commas?+
The JSON specification (RFC 8259) defines a strict grammar where commas separate items, not terminate them. JavaScript is more lenient and allows trailing commas, which is why this mistake is so common when copying data between the two.
Can I use single quotes in JSON?+
No. The JSON specification requires all keys and string values to use double quotes. Single quotes and backticks are not valid. This differs from JavaScript and Python, where single quotes are common.
Why does JSON.parse fail at position 0?+
This usually means the string starts with a Byte Order Mark (BOM) or other invisible character. Strip the BOM before parsing, or check that the API response is actually JSON and not an HTML error page.
Does JSON support comments?+
No. Standard JSON does not support any form of comments. Files like tsconfig.json and VS Code settings use JSONC (JSON with Comments), which is a superset. Strict JSON parsers will reject comments.
What is the difference between JSON formatting and validation?+
Formatting (beautifying) adds indentation to make JSON readable. Validation checks whether the JSON conforms to the specification. Minification strips whitespace for smaller file size. A good tool does all three.