Skip to content
Future Forge ToolsWeb Tools ProOpen free tools

Developer Notes

JSON Formatting and Validation: Common Errors and How to Fix Them

The JSON errors that break parsers most often, from trailing commas and single quotes to unquoted keys and comments, with fixes and a validation routine.

JSON Formatting and Validation: Common Errors and How to Fix Them
On this page

JSON looks simple, and mostly it is. But its rules are stricter than the JavaScript it resembles, and a single misplaced character makes the whole document unreadable to a parser. The error message you get back is often unhelpful, something like “Unexpected token” at a position that seems unrelated to the real problem.

The good news is that almost all JSON errors come from a short list of mistakes. Learn to spot them and you’ll fix most broken JSON in seconds.

Try it: JSON FormatterValidate JSON with exact error line and column, beautify it with your preferred indentation, sort keys or minify it.

Open tool — free

The rules that trip people up

A valid JSON document is a single value: an object, array, string, number, true, false or null. Inside it:

  • Object keys must be strings in double quotes.
  • Strings use double quotes only.
  • Items are separated by commas, with no comma after the last item.
  • There are no comments.
  • Values like undefined, NaN, Infinity and functions are not allowed.

Almost every error below breaks one of those five rules.

The most common JSON errors

1. Trailing commas

The number one cause of invalid JSON, especially when you edit by hand and delete the last item in a list.

Invalid: {"name": "Ada", "role": "admin",}

Valid: {"name": "Ada", "role": "admin"}

The same applies to arrays: [1, 2, 3,] is invalid. JavaScript tolerates trailing commas, which is exactly why they sneak into JSON files.

2. Single quotes

Invalid: {'name': 'Ada'}

Valid: {"name": "Ada"}

This often happens when JSON is copied from Python output, which prints dictionaries with single quotes. In Python, use json.dumps() to produce real JSON instead of printing the dictionary.

3. Unquoted keys

Invalid: {name: "Ada"}

Valid: {"name": "Ada"}

Fine in a JavaScript object literal, not in JSON. If you build JSON from JavaScript, use JSON.stringify() rather than writing it by hand.

4. Comments

Invalid: {"port": 8080 // default}

JSON has no comment syntax. Some editors and config systems use JSONC or JSON5, which allow comments, and files like VS Code settings rely on that. But if a strict parser reads the file, comments break it. If you need notes in standard JSON, a common workaround is a regular key such as "_comment": "default port", as long as the program reading the file ignores unknown keys.

5. Unescaped characters inside strings

Double quotes, backslashes and line breaks inside a string must be escaped.

Invalid: {"quote": "She said "hi""}

Valid: {"quote": "She said \"hi\""}

Windows file paths are a classic trap: "C:\Users\ada" is invalid because \U isn’t a valid escape. Write "C:\\Users\\ada" or use forward slashes where the program allows.

6. Missing commas or brackets

Two values with no comma between them, or an object that’s never closed. These usually come from copy-pasting fragments together. A formatter’s indentation makes them obvious, because everything after the mistake ends up nested at the wrong level.

7. Non-JSON values

undefined, NaN, Infinity, dates like new Date() and hex numbers like 0xFF aren’t valid JSON. Use null, a string, or a plain number instead. Dates are usually stored as ISO 8601 strings, such as "2026-03-14T09:30:00Z", or as Unix timestamps. The Unix timestamps guide explains the trade-offs.

8. Smart quotes and invisible characters

Copying JSON from a word processor, chat app or web page can replace straight quotes with curly ones, which parsers reject. A byte order mark (BOM) at the start of a file or a non-breaking space can cause similar confusion. If the JSON looks perfect but won’t parse, retype the quotes around the reported position.

How to find and fix errors fast

Reading a minified 2,000-line JSON response by eye isn’t realistic. Let a validator do the searching. Our free JSON formatter runs in your browser, so the data you paste isn’t uploaded anywhere, which matters when you’re debugging API responses with tokens or customer data in them.

  1. Paste the JSON into the formatter.
  2. Validate it. If it’s invalid, note the exact line and column in the error.
  3. Go to that position and check the character just before it as well. Parsers often report an error at the point where they got confused, which can be one token after the real mistake.
  4. Fix the issue and validate again. Repeat until it parses, since one error can hide another.
  5. Beautify with your preferred indentation to review the structure, sort keys if you want stable diffs, or minify it for production.

Once it’s valid, you can also compare two versions with a text diff to see exactly what changed between API responses or config files.

Prevent errors instead of fixing them

  • Generate JSON with code. JSON.stringify() in JavaScript, json.dumps() in Python and the equivalents in other languages always produce valid output. Hand-written JSON is where most errors come from.
  • Use an editor with JSON support. Most code editors highlight errors as you type.
  • Validate in CI for config files that live in a repository. A broken config caught before deploy is a non-event.
  • Consider a schema. JSON Schema lets you check not just syntax but structure: required fields, types and allowed values.

When JSON isn’t the right format

If you’re editing large tables of flat records by hand, JSON is a painful format to work in. Spreadsheets and CSV are easier for that, and you can convert between them. A CSV, JSON and Excel converter turns JSON arrays into a table you can edit and back again. The CSV vs JSON vs Excel comparison covers when each format makes sense.

The takeaway

When JSON won’t parse, check for trailing commas, single quotes, unquoted keys and comments first. Those four cause most failures. Use a validator to get the exact line and column, look just before that position, and generate JSON with code whenever you can so the problem doesn’t come back.

Frequently asked questions

Why is my JSON invalid when it looks fine?

The usual culprits are a trailing comma after the last item, single quotes instead of double quotes, unquoted keys or a comment. A validator that reports the exact line and column will point you straight at it.

Does JSON allow comments?

No. Standard JSON has no comment syntax, so // and /* */ cause parse errors. Some tools accept JSON variants with comments, such as JSONC or JSON5, but a strict parser will reject them.

What is the difference between JSON and a JavaScript object?

JSON is a text format with stricter rules: keys must be double-quoted strings, strings use double quotes, and there are no trailing commas, comments, functions or undefined values. A JavaScript object literal allows all of those.

Free tools for this

admin

Writes practical guides for Future Forge Tools, where every tool runs privately in your browser.