How to Fix Common JSON Errors: A Developer’s Survival Guide

Published: (January 3, 2026 at 10:49 AM EST)
7 min read
Source: Dev.to

Source: Dev.to

Introduction

Have you ever sat up late, staring at a red error message that simply reads “Unexpected token”? JSON (JavaScript Object Notation) is the lingua‑franca for data exchange, but even a stray comma or the wrong type of quotes can break everything. If you want a quick sanity check, run your payload through a JSON formatter before you start debugging.

In this guide I’ll unpack the most frequent JSON errors I’ve seen over the years, show you exactly how to identify them, and offer practical fixes you can apply right away. Whether you’re just getting started with APIs or you’ve been integrating services for years, knowing how to read and correct these errors will save you a lot of frustration.

1. Trailing Commas

Problem – Leaving a comma after the last item in an object or array. Unlike JavaScript, JSON doesn’t tolerate trailing commas, so parsers throw errors like “Unexpected token ‘}’”.

Invalid JSON (trailing comma)

{
  "name": "John Doe",
  "age": 30,
  "city": "New York",
}

Fix

Remove the final comma.

{
  "name": "John Doe",
  "age": 30,
  "city": "New York"
}

How to avoid it

  • Use a JSON validator or linter integrated into your editor; many will highlight trailing commas automatically.
  • Configure your formatter (Prettier, ESLint, etc.) to remove trailing commas on save.
  • When copying objects from JavaScript, run them through a validator (e.g., CodeItBro’s JSON validator) before using them in configuration files or API calls.

2. Wrong Quote Characters

Problem – JSON requires double quotes for all strings and property names. Single quotes are legal in JavaScript but cause “unexpected character” errors in JSON.

Invalid JSON (single quotes)

{
  'name': 'John Doe',
  'active': true
}

Fix

Replace all single quotes with double quotes.

{
  "name": "John Doe",
  "active": true
}

Why this matters

  • Consistent double quotes keep your JSON valid across languages and platforms.
  • It avoids subtle bugs when different parsers interpret single quotes differently.

3. Unquoted Property Names

Problem – In JavaScript object literals you can omit quotes around keys, but JSON never allows that.

Invalid JSON (unquoted keys)

{
  name: "John Doe",
  age: 30
}

Fix

Wrap every property name in double quotes.

{
  "name": "John Doe",
  "age": 30
}

Tips

  • Modern editors usually highlight unquoted keys when you enable JSON syntax mode.
  • Turn on JSON linting to catch these mistakes early.

4. Missing Commas Between Items

Problem – Forgetting a comma between properties (or array elements) results in errors such as “Unexpected string”.

Invalid JSON (missing comma)

{
  "firstName": "John"
  "lastName": "Doe"
}

Fix

Add the missing comma.

{
  "firstName": "John",
  "lastName": "Doe"
}

Quick check

When an error points to a line that looks fine, inspect the previous line for a missing comma.

5. Mismatched Brackets

Problem – Every { must have a matching }, and every [ must have a matching ]. In long or deeply nested JSON it’s easy to overlook a missing closing bracket, leading to “Unexpected end of JSON input”.

Invalid JSON (missing closing brace)

{
  "users": [
    {"name": "Alice"},
    {"name": "Bob"}
  ],
  "total": 2

Fix

Close all structures properly.

{
  "users": [
    {"name": "Alice"},
    {"name": "Bob"}
  ],
  "total": 2
}

Tips

  • Most editors highlight matching brackets.
  • Use a JSON formatter/pretty‑printer to indent nested levels; mismatches become obvious.

6. Invalid Number Formats

Problem – JSON only supports standard decimal numbers. Leading zeros, hexadecimal notation, and special values like NaN or Infinity are illegal.

Invalid JSON (bad numbers)

{
  "quantity": 007,
  "price": 0xFF,
  "discount": NaN
}

Fix

Use proper decimal numbers; replace unsupported values with null (or a valid number).

{
  "quantity": 7,
  "price": 255,
  "discount": null
}

Note

Scientific notation (e.g., 1.5e10) is valid JSON.

7. Unsupported Data Types

Problem – JSON is limited to six data types: string, number, boolean, null, array, object. Values such as undefined, functions, Date objects, regular expressions, and comments are not allowed.

Invalid JSON (unsupported types)

{
  "name": "John",
  "age": undefined,
  "callback": function() { return true; },
  "created": new Date()
}

Fix

Convert or remove unsupported values.

{
  "name": "John",
  "age": null,
  "callback": null,
  "created": "2025-01-30T10:30:00Z"
}

Conversion checklist

  • Replace undefined with null.
  • Convert functions to strings or drop them.
  • Represent dates as ISO‑8601 strings.
  • Turn regular expressions into plain strings.
  • Use toJSON() on custom objects to serialize them.

8. Comments in JSON

Problem – JSON does not permit comments. Adding /* … */ or // will cause parsers to reject the file.

Invalid JSON (with comments)

{
  "name": "John Doe",

  /* Age in years */
  "age": 30
}

Options for including notes

  1. Add a dedicated comment field (e.g., _comment) that holds a string.
  2. Keep documentation separate (README, docs folder, etc.).
  3. Use JSONC (JSON with comments) only when your tooling explicitly supports it.

9. Escape Sequences in Strings

Strings in JSON can include escape sequences (e.g., \n, \t, \\, \").
Make sure any backslashes are properly escaped; otherwise the parser will treat them as invalid characters.

Final Tips

  • Validate early – run every JSON payload through a validator or linter before committing it.
  • Enable editor support – most modern IDEs have built‑in JSON syntax highlighting, bracket matching, and linting.
  • Automate formatting – configure a formatter (Prettier, jq, etc.) to run on save or as part of your CI pipeline.

By keeping these common pitfalls in mind, you’ll spend far less time chasing cryptic “Unexpected token” errors and more time building great applications. Happy coding!

Common JSON Errors and How to Fix Them

When working with JSON, a tiny typo can cause hours of debugging. Below are some of the most frequent pitfalls, why they happen, and practical ways to avoid or resolve them.

1. Invalid Escape Sequences

Only a specific set of escape sequences is allowed in JSON strings (e.g., \n for newline, \t for tab, Unicode escapes like \u00A9). Any unrecognised escape will trigger a parsing error.

Invalid JSON with bad escape sequences

{
  "path": "C:\\new\\folder",
  "message": "Line 1\\xLine 2"
}

Valid JSON using correct escapes

{
  "path": "C\\\\new\\\\folder",
  "message": "Line 1\\nLine 2"
}

Tip: Keep a list of the valid escape sequences handy, or rely on your editor’s syntax‑highlighting to spot invalid ones.

2. Duplicate Property Names

While some parsers tolerate duplicate keys, the JSON specification does not define how they should be handled. Different implementations may keep the first value, the last value, or throw an error, leading to unpredictable results.

Problematic JSON with duplicate keys

{
  "name": "John Doe",
  "age": 30,
  "name": "Jane Doe"
}

Corrected JSON – each key appears only once

{
  "firstName": "John",
  "lastName": "Doe",
  "age": 30
}

Advice: Use meaningful, unique property names to keep your data predictable across platforms.

3. Debugging Large JSON Files

When a JSON file is big and the error isn’t obvious, a systematic approach can save you a lot of time.

Techniques

  • Use a JSON validator – Online tools highlight errors and show line numbers.
  • Format the file first – Proper indentation reveals structural issues such as mismatched brackets.
  • Binary search your JSON – Comment out (or temporarily remove) half the content, validate the remaining half, and repeat until you isolate the problem.
  • Check the encoding – Ensure the file is UTF‑8; hidden BOMs or special characters can cause cryptic errors.
  • Validate programmatically – Wrap parsing in a try/catch block and log the error message and position.

4. Preventing Errors – Best Practices

PracticeWhy It Helps
Schema validationGuarantees that data conforms to an expected shape.
Automate formattingConfigure your editor to auto‑format JSON on save.
Use lintingIntegrate a linter (e.g., ESLint with JSON plugins) to catch issues as you type.
Code‑review JSONTreat configuration changes as code and review them carefully.
Version controlTrack JSON files in Git (or another VCS) to pinpoint when problems were introduced.
Generate JSON programmaticallyLibraries/serialization functions won’t forget commas or misquote strings.
Test thoroughlyInclude JSON validation in your automated test suite.

Closing Thoughts

Getting JSON right isn’t glamorous, but it’s essential. By remembering the core rules—no trailing commas, double‑quoted strings and keys, proper numbers, valid escape sequences, and no duplicate keys—you can eliminate most syntax errors before they reach production. When issues do crop up, systematic debugging and good tooling will help you find and fix them quickly.

Have you battled any of these errors recently?
Which tools or techniques saved your day? Share your stories in the comments, and let’s help each other avoid late‑night JSON headaches.

If you found this guide useful, bookmark it or share it with your team. Happy coding!

Back to Blog

Related posts

Read more »