Skip to main content
Developer7 min readPhoto of Muhammad Irfan LiaqatBy Muhammad Irfan Liaqat

JSON Formatting Guide for Beginners

The six JSON value types, the five mistakes that break most JSON files, and what each parse error means, with examples tested in the browser.

The same user data shown as one cramped line of JSON, then formatted with indentation so each field is readable.

JSON is the text format most web APIs, config files and data exports use to store structured data. It has only six kinds of value and a handful of strict rules. Once you know where those rules bite, a parse error tells you exactly what to fix.

This guide covers the rules from the JSON standard, the mistakes that break most files, and what the error messages actually mean. Every error message below was produced by pasting the example into the JSON Formatter in Chrome.

What JSON Looks Like

{
  "name": "Sarah",
  "age": 28,
  "member": true,
  "city": null,
  "hobbies": ["reading", "hiking"]
}

That is one object, wrapped in curly braces, holding five name/value pairs. Each name is a string in double quotes, followed by a colon and a value. Commas separate the pairs.

JSON is defined by RFC 8259, the internet standard for the format, and by ECMA-404. Both describe the same grammar.

The Six Kinds of Value

RFC 8259 defines four simple types and two structured types:

TypeExampleRule to remember
String"hello"Double quotes only
Number42, -3.5, 1e6No leading zeros, no NaN or Infinity
Booleantrue, falseLowercase
NullnullLowercase
Array[1, 2, 3]An ordered list of values
Object{"key": "value"}Names must be strings

Arrays and objects can hold any of the six, including each other. That nesting is how JSON describes something like a list of users, each with a list of orders.

The Five Mistakes That Break Most JSON

JSON looks like JavaScript, but it is stricter. These are the differences that trip people up, with the message Chrome gives for each.

1. A trailing comma

{"name": "Alice", "age": 30,}

Chrome says: Expected double-quoted property name in JSON at position 28. After a comma the parser expects another name, finds a closing brace and stops. Delete the last comma.

2. Single quotes

{'name': 'Alice'}

Chrome says: Expected property name or '}' in JSON at position 1. JSON strings, including names, must use double quotes.

3. Unquoted names

{name: "Alice"}

This gives the same error as single quotes, at the same position. JavaScript allows bare names in objects, but JSON does not.

4. Comments

{"name": "Alice" // the account owner
}

Chrome says: Expected ',' or '}' after property value in JSON at position 17, pointing at the first slash. The JSON standard has no comment syntax. Some tools accept comments in their own config files (VS Code's settings, for example, use a variant called JSONC), but a standard parser rejects them.

5. Values JavaScript has and JSON doesn't

NaN, Infinity, undefined and numbers with leading zeros such as 007 are all invalid. {"zip": 007} fails with Unexpected number in JSON at position 9. If you build JSON by hand from a spreadsheet, watch for ZIP codes and phone numbers that start with 0. Store them as strings: "007".

How to Read a Parse Error

The position in an error message counts characters from the start of the text, beginning at 0. Chrome also adds the line and column. For a trailing comma on line 40 of a long file, go to that line and look just before the reported column.

When the position points at a character that looks fine, the real mistake is usually just before it. A missing comma between two values, for example, is only noticed when the parser reaches the second value.

Duplicate Names Are Allowed, but Risky

This parses without any error:

{"role": "user", "role": "admin"}

RFC 8259 says names within an object should be unique, and that software handles duplicates in different ways. JavaScript keeps the last one, so the result is {"role": "admin"}. Other parsers may keep the first value or reject the object. If two systems read the same file, a duplicate name can mean they see different data.

Formatting vs Minifying

The data is the same either way. Formatting adds line breaks and indentation so people can read it. Minifying removes them so the file is smaller to send.

An API response often arrives minified:

{"users":[{"id":1,"name":"Alice","active":true},{"id":2,"name":"Bob","active":false}]}

Formatted with a 2-space indent, the structure is easy to see:

{
  "users": [
    {
      "id": 1,
      "name": "Alice",
      "active": true
    },
    {
      "id": 2,
      "name": "Bob",
      "active": false
    }
  ]
}

The JSON Formatter does both. Paste your JSON and choose Format, with a 2- or 4-space indent, or Minify. If the input is invalid, the output box shows your browser's error message in red, with the position. The text never leaves your browser, which matters when you are debugging a response that contains customer data or API keys.

Where You'll Meet JSON

  • API responses: most web APIs send and receive JSON.
  • Config files: package.json and tsconfig.json are two common examples.
  • JWT tokens: the header and payload of a JWT are Base64url-encoded JSON. The JWT Decoder turns them back into readable JSON.
  • Spreadsheet exports: the CSV to JSON Converter turns each CSV row into a JSON object, using the header row for the names.

JSON Compared With Other Formats

FeatureJSONXMLYAMLCSV
NestingYesYesYesNo
CommentsNoYesYesNo
Data types6 built inText onlyInferredText only
Good forAPIs, data exchangeDocuments, legacy systemsConfig filesTables

JSON became the default for web APIs because it is less verbose than XML and every major language has a JSON parser built in or in its standard library.

Takeaways

  • JSON has six value types: string, number, boolean, null, array and object.
  • Use double quotes for every string and every name.
  • No trailing commas, no comments, no NaN and no leading zeros.
  • When a parse error points at a valid-looking character, check the character just before it.
  • Keep names in an object unique, because parsers disagree about duplicates.

Paste a file that won't parse into the JSON Formatter to see the error and its position.