How-to Guides

How to Format and Validate JSON (and Fix the Most Common Errors)

By AllToolz4U Team · · Updated · 5 min read

How to Format and Validate JSON (and Fix the Most Common Errors)

You paste a block of JSON into your app and get a message like Unexpected token } in JSON at position 214. The data looks fine to you. Somewhere, though, there is one character the parser does not accept, and finding it in a minified single line can feel impossible.

This guide explains the rules of JSON in plain language, shows the mistakes that cause nearly all errors, and gives you a repeatable way to find the problem quickly.

What JSON is, in one minute

JSON (JavaScript Object Notation) is a text format for structured data. It is used by web APIs, configuration files and many apps because it is easy for both people and programs to read. It is built from just a few pieces:

  • Objects in curly braces, holding key-value pairs: {"name": "Sam", "age": 30}
  • Arrays in square brackets, holding ordered lists: [1, 2, 3]
  • Values: strings in double quotes, numbers, true, false, null, or another object or array.

The format is defined in a standard (RFC 8259) and is stricter than JavaScript object syntax. Many errors happen because people write JavaScript-style data and expect it to be valid JSON.

Format first, then validate

Formatting (also called pretty-printing or beautifying) adds line breaks and indentation so you can see the structure. Do it before you start hunting for errors, because mismatched brackets and misplaced items become obvious when each level is indented.

Validating checks that the text follows the rules. A good validator tells you the line and character where parsing failed. Keep in mind that this is the spot where the parser gave up, which is often slightly after the real mistake.

You can use a browser-based JSON formatter and validator, your code editor (most format JSON with a shortcut), or the command line. For example, python3 -m json.tool file.json formats a file and prints an error if it is invalid.

The six most common JSON errors

1. A trailing comma

This is the number one cause. JavaScript allows a comma after the last item, JSON does not.

{"a": 1, "b": 2,}   <- invalid
{"a": 1, "b": 2}    <- valid

2. Single quotes instead of double quotes

Strings and keys must use double quotes. Single quotes make the data invalid.

{'name': 'Sam'}     <- invalid
{"name": "Sam"}     <- valid

3. Keys without quotes

In JavaScript you can write {name: "Sam"}. In JSON every key must be a quoted string.

4. Comments

JSON has no comment syntax. Both // note and /* note */ cause errors. If you need notes in a configuration file, use a format that supports comments or add a field such as "_comment".

5. Missing commas or brackets

Forgetting a comma between two items, or leaving a bracket or brace unclosed, breaks everything after it. Indentation from a formatter usually shows where the structure goes wrong.

6. Invalid values and numbers

  • Numbers cannot have leading zeros (007) and cannot be written in hexadecimal (0xFF).
  • NaN, Infinity and undefined are not valid JSON values.
  • Special characters inside strings such as line breaks and tabs must be escaped (\n, \t), and a literal double quote must be written as \".

A method for finding the error fast

  1. Pretty-print the data and look at the indentation. A sudden jump in indentation points to a missing or extra bracket.
  2. Go to the reported position, then look a few characters before it. Check commas, quotes and brackets in that area.
  3. Cut the problem in half. If the data is large, remove half of it (keeping the structure valid), validate, and repeat. This narrows the problem to a small section in a few steps.
  4. Check the encoding. The file should be UTF-8. A hidden byte order mark at the start or strange copy-pasted characters, such as curly "smart quotes" from a word processor, cause confusing errors.
  5. Check where the data came from. Sometimes the response is not JSON at all, for example an HTML error page returned by a server. Print the first few characters.

Validation beyond syntax

Syntax validation only tells you the text is well-formed. It does not tell you the data makes sense, for example that age is a number or that email exists. For that, use a JSON Schema, which describes the expected structure and lets validators in most programming languages check it.

Handling JSON safely in code

  • Always wrap parsing in error handling (try/catch or the equivalent) and log the message and position.
  • Never evaluate JSON as code. Use the language's proper JSON parser.
  • Be careful when pasting sensitive data such as API keys or customer records into online tools. Use a formatter that works in your browser or on your own machine if the data is private.

Beautify or minify: which one do you need?

Pretty-printed JSON is for people: indentation and line breaks make it readable. Minified JSON removes all optional whitespace, which makes the file smaller and quicker to send. Use pretty-printed data while you write and debug, and minified data when you deliver it to an API or a browser. Both contain exactly the same information and any valid parser reads them the same way.

How JSON compares with other formats

  • XML is more verbose but supports attributes and comments. It is still common in older systems.
  • YAML is easier to read and allows comments, which makes it popular for configuration files. It is also more sensitive to indentation mistakes.
  • CSV is great for flat tables of rows and columns, but it cannot represent nested data.

Quick checklist

  • Double quotes only, for keys and strings.
  • No trailing commas, no comments.
  • Every opening bracket has a closing partner.
  • Numbers without leading zeros; no NaN or undefined.
  • UTF-8 text, no stray characters.

Frequently Asked Questions

Why does JSON not allow trailing commas?

The JSON standard defines its grammar strictly, and a comma must be followed by another value. JavaScript later relaxed this for its own syntax, but JSON itself stayed strict, which keeps parsers simple and consistent.

Can JSON have comments?

No. Comments are not part of the JSON standard. Use a different format that supports comments, or store notes in a dedicated field.

What is the difference between JSON formatting and validation?

Formatting only changes the layout (indentation and line breaks) to make the data readable. Validation checks that the text follows the JSON rules. Invalid JSON often cannot be formatted until the errors are fixed.

Is it safe to paste JSON into an online formatter?

Only if the data is not sensitive, or the tool processes it inside your browser. For private data like tokens or personal records, use a local editor or command line tool.

Keep reading