JSON Explained: How to Format, Validate and Fix Common Errors

JSON (JavaScript Object Notation) is the plain-text format most websites, apps and APIs use to pass data around. Its rules are short but strict: one stray comma and a program rejects the whole file. This guide covers the rules, the errors you'll meet most often, and a quick routine for fixing them.

What JSON looks like

Here is a small JSON object describing an employee:

{"name": "Sara", "city": "Dubai", "age": 31, "active": true, "skills": ["Excel", "SQL"], "manager": null}

It's built from two structures and a handful of value types:

  • Objects, in curly braces, hold name and value pairs: {"city": "Dubai"}.
  • Arrays, in square brackets, hold ordered lists: ["Excel", "SQL"].
  • Values can be a string, a number, true, false, null, or another object or array, nested as deeply as you need.

The format is defined in RFC 8259, a short and readable standard.

The syntax rules

  • Every name (key) must be a string in double quotes: "age": 31, never age: 31.
  • Strings use double quotes only. Inside a string, write a double quote as \", a backslash as \\ and a line break as \n. A Windows path becomes "C:\\Users\\Sara".
  • Numbers are plain: 31, -4.5, 1.2e6. No leading zeros (007), no thousands separators, no currency signs, and no NaN or Infinity.
  • true, false and null must be lowercase.
  • Commas go between items, never after the last one.
  • There are no comments.
  • Spaces, tabs and line breaks between items don't matter, which is why the same data can be formatted or minified.
  • Names within an object should be unique. The standard warns that software behaves unpredictably when they aren't.
  • JSON exchanged between systems must be UTF-8, so Arabic, Urdu or accented text is fine as long as the file is saved as UTF-8.

Common errors and how to fix them

MistakeBrokenFixed
Trailing comma{"name": "Sara", "age": 31,}{"name": "Sara", "age": 31}
Single quotes{'name': 'Sara'}{"name": "Sara"}
Unquoted name{name: "Sara"}{"name": "Sara"}
Comment{"a": 1 /* note */}{"a": 1}
Missing comma{"a": 1 "b": 2}{"a": 1, "b": 2}
Python-style values{"active": True, "boss": None}{"active": true, "boss": null}

Error messages vary between browsers. Chrome, for example, reports the trailing comma above as Expected double-quoted property name in JSON at position 27 (line 1 column 28), because after a comma it expects another name. Positions count characters from 0, and the real mistake is usually at that spot or just before it.

Trailing commas are usually left behind after deleting the last item in a list. Single quotes and unquoted names are fine in JavaScript code, so they sneak in when people copy from a script; Chrome reports both as Expected property name or '}'.

Comments are accepted by some tools, such as VS Code settings files, which use a variant called JSONC, but a standard parser rejects them. If you need a note, store it as data: "_comment": "Prices in AED".

Python output is a frequent culprit. Printing a Python dictionary gives True, None and single quotes. Use json.dumps(), which writes real JSON.

Smart quotes sneak in when JSON is pasted from Word, Outlook or a chat app. Curly quotes (“ ”) look right but aren't valid, so replace them with straight double quotes. Likewise, a double quote inside a string must be escaped: "He said \"yes\"".

Formatted vs minified JSON

Formatting, also called pretty-printing, adds line breaks and indentation so people can see the structure. Minifying removes every space and line break outside strings, so the file is smaller to send. The data is identical either way.

The employee example, formatted with two-space indentation:

{ "name": "Sara", "city": "Dubai", "age": 31, "active": true, "skills": [ "Excel", "SQL" ], "manager": null }

Minified, it becomes {"name":"Sara","city":"Dubai","age":31,"active":true,"skills":["Excel","SQL"],"manager":null}. The formatted version is 131 bytes and the minified one is 93, about 29% smaller. On large API responses that saving adds up, although many servers also compress responses, which narrows the gap.

A quick routine for fixing broken JSON

  1. Paste the JSON into a validator and note the position, line and column of the error.
  2. Minified JSON sits on a single line, so rely on the position or column number rather than the line.
  3. Look at the character at that position and the one just before it. Missing commas, extra commas and wrong quotes are the usual suspects.
  4. Fix one error at a time and validate again. One mistake can hide the next.
  5. If the JSON came from an email or chat, retype the quotes, because smart quotes are hard to spot.

Using the SAA Tool JSON Formatter

Paste your JSON into the JSON Formatter and click Format to indent it with two spaces, or Minify to strip the whitespace. Valid input shows "Valid JSON"; invalid input shows your browser's error message with the position, so you can find the problem. Copy puts the result on your clipboard. Everything runs in your browser, so API responses and config files aren't uploaded anywhere.

  • It tells you where the error is but doesn't fix it for you.
  • It checks syntax only, not whether the data matches what an API expects. That needs a JSON Schema validator.
  • Numbers above 9,007,199,254,740,991 lose precision when reformatted, because browsers store numbers as 64-bit floating point: 12345678901234567890 comes back as 12345678901234567000. If your data has very long numeric IDs, don't save the reformatted version over the original.
  • With duplicate names, only the last one survives: {"a": 1, "a": 2} becomes {"a":2} without a warning.
  • Indentation is fixed at two spaces.

If your JSON contains tokens or encoded strings, our guide to Base64, URL encoding and JWTs explains those formats.

Frequently asked questions

Is JSON the same as a JavaScript object?

No. JSON's syntax comes from JavaScript, but it's stricter: names must be double-quoted, and comments, trailing commas and functions aren't allowed. A lot of JavaScript object code isn't valid JSON.

Can JSON contain comments?

Not standard JSON. Variants such as JSONC and JSON5 allow them, but only tools that specifically support those formats will accept the file.

Does the order of keys matter?

Not to the standard, which treats an object as an unordered collection. Most tools keep the order you gave, though browsers move names that look like whole numbers, such as "2", to the front. Code shouldn't depend on key order.

Why does my JSON work in one app but fail in another?

Some programs use lenient parsers that quietly accept trailing commas or comments. A strict parser, like the one in every browser, doesn't. If a file must work everywhere, make sure it passes a strict validator.

More guides