Workspace

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

JSON is the format most web APIs, config files and data exports use. It is simple to read when it is formatted well and almost impossible to read when it arrives as one long line. It is also strict: a single misplaced character makes the whole document invalid. This guide explains what valid JSON looks like, how to format it and how to fix the errors people run into most often.

What valid JSON looks like

JSON has only a few building blocks. An object is a set of key and value pairs inside curly braces. An array is an ordered list inside square brackets. Values can be strings, numbers, true, false, null, or other objects and arrays. The rules are what trip people up:

Why formatting matters

Formatting, also called pretty-printing or beautifying, adds line breaks and indentation so the structure is visible. It does not change the data. A formatted document is easier to scan, easier to review in a pull request and much easier to check against documentation. The opposite process, minifying, removes the extra whitespace to make the file smaller for sending over a network.

{"user":{"id":42,"name":"Asha","roles":["editor","admin"]},"active":true}

The same data, formatted with two-space indentation, reads like this:

{
  "user": {
    "id": 42,
    "name": "Asha",
    "roles": [
      "editor",
      "admin"
    ]
  },
  "active": true
}

Format and validate JSON →

The most common JSON errors and how to fix them

1. Trailing comma

A comma after the last item, as in {"a": 1, "b": 2,}, is the most common mistake, often left behind after deleting a line. Remove the final comma.

2. Single quotes or unquoted keys

JavaScript lets you write {name: 'Asha'}, but JSON does not. Wrap every key and string in double quotes: {"name": "Asha"}.

3. Comments

Config files that look like JSON often contain comments. A strict JSON parser will reject them. Remove the comments, or use a format that supports them, such as YAML or JSON5, if your tool allows it.

4. Unescaped characters inside strings

A double quote inside a string must be written as \", a backslash as \\ and a line break as \n. Pasting text that contains raw line breaks inside a string is a common cause of “unexpected token” errors.

5. Missing or extra brackets

Every opening brace or bracket needs a matching closing one. Formatting helps here: when the indentation looks wrong, the problem is usually near that line.

6. Hidden characters

Text copied from word processors or web pages can include curly quotes, non-breaking spaces or an invisible byte order mark at the start. If everything looks right but parsing fails, retype the quotes or paste the text into a plain-text editor first.

A simple workflow

  1. Paste the JSON into a formatter and run it. If it is valid, you get indented output immediately.
  2. If it is invalid, read the error message, then look at the place it points to and the line before it.
  3. Fix one error at a time and run it again, because one mistake can hide another.
  4. When it validates, copy the result. If you need to compare two versions, format both with sorted keys, so that the same data always appears in the same order.

Remember that a formatter running on a web service receives what you paste. Replace real passwords, tokens and personal data with dummy values before you use any online tool.

Next steps

Once your JSON is valid you can compare two documents with the JSON diff tool, turn an array of objects into a spreadsheet with JSON to CSV, or generate TypeScript types from a sample. When you are ready to ship, minify it to shrink the payload.

Tools mentioned in this guide

More guides