Cheatsheets

JSON Cheat Sheet: Syntax, Data Types, and Common Errors

March 9, 20265 min readNebulaTool

JSON (JavaScript Object Notation) is the most widely used data interchange format on the web. This cheat sheet covers every syntax rule, data type, and common error you will encounter when reading or writing JSON. Bookmark it and come back whenever you need a quick reference.

JSON Syntax Rules

The entire JSON specification fits on a single page, but a surprising number of bugs come from forgetting one of these rules:

  1. Data is in key-value pairs. Keys must be strings wrapped in double quotes.
  2. Objects use curly braces. { } contain an unordered set of key-value pairs.
  3. Arrays use square brackets. [ ] contain an ordered list of values.
  4. Values are separated by commas. No trailing comma is allowed after the last item.
  5. Strings must use double quotes. Single quotes and unquoted keys are not valid JSON.
  6. No comments are allowed. JSON does not support // or /* */ style comments.
  7. No trailing commas. Unlike JavaScript, a comma after the final element is a syntax error.

A minimal valid JSON document is as simple as:

{
  "status": "ok"
}

A bare value like 42, "hello", true, null, or an array [1, 2, 3] is also valid. The spec allows any JSON value at the top level.

Data Types

JSON supports exactly six data types. There are no others, no dates, no undefined, and no functions.

TypeExampleNotes
String"hello world"Must use double quotes. Supports \n, \t, \", \\, and \uXXXX escapes.
Number42, -3.14, 1.5e10No leading zeros (except 0.x). No hex, octal, NaN, or Infinity.
Booleantrue, falseLowercase only. True or TRUE are invalid.
NullnullLowercase only. Represents an empty or absent value.
Object{ "key": "value" }Keys must be unique strings. Values can be any type.
Array[1, "two", true, null]Ordered list. Elements can be mixed types.

For full details on each type, see the MDN JSON documentation.

Objects

An object is an unordered collection of key-value pairs:

{
  "name": "NebulaTool",
  "version": 2,
  "isPublic": true,
  "homepage": null
}

Rules to remember:

  • Every key must be a double-quoted string.
  • Keys should be unique. Duplicate keys are technically parsed, but behavior varies across implementations.
  • Values can be any of the six JSON data types, including other objects and arrays.

Arrays

An array is an ordered list of values:

{
  "colors": ["red", "green", "blue"],
  "matrix": [[1, 2], [3, 4]],
  "mixed": [42, "text", true, null, {"nested": "object"}]
}

Arrays can contain values of different types, and they can be nested to any depth.

Nesting

JSON structures can be nested to represent complex data. Objects can contain arrays, arrays can contain objects, and both can be nested as deeply as needed:

{
  "user": {
    "name": "Alice",
    "contacts": {
      "email": "alice@example.com",
      "phones": [
        { "type": "home", "number": "555-1234" },
        { "type": "work", "number": "555-5678" }
      ]
    },
    "tags": ["admin", "active"]
  }
}

Keep nesting shallow when possible. Deeply nested JSON is harder to read, harder to query, and more error-prone. If you regularly go beyond three or four levels, consider restructuring your data.

Common Errors and How to Fix Them

Trailing Commas

// INVALID
{ "a": 1, "b": 2, }

// VALID
{ "a": 1, "b": 2 }

JavaScript and many config formats allow trailing commas. JSON does not. Remove the comma after the last element.

Single Quotes

// INVALID
{ 'name': 'NebulaTool' }

// VALID
{ "name": "NebulaTool" }

Always use double quotes for both keys and string values.

Unquoted Keys

// INVALID
{ name: "NebulaTool" }

// VALID
{ "name": "NebulaTool" }

Every key must be a quoted string, even if it looks like a valid identifier.

Comments

// INVALID (JSON has no comments)
{
  "debug": true  // enable debug mode
}

// VALID
{
  "debug": true
}

If you need comments in configuration, consider JSONC (JSON with Comments) or YAML. Standard JSON parsers will reject any comment syntax.

Unescaped Special Characters

// INVALID
{ "path": "C:\new\folder" }

// VALID
{ "path": "C:\\new\\folder" }

Backslashes, double quotes, and control characters inside strings must be escaped.

Using undefined, NaN, or Infinity

These are JavaScript values, not JSON values. Use null for absent values. There is no JSON representation for NaN or Infinity. Use a string or a sentinel number if you need to represent them.

Quick Reference Table

What you wantCorrect JSON
Empty object{}
Empty array[]
Empty string""
Null valuenull
Boolean truetrue
Nested object{ "a": { "b": 1 } }
Array of objects[{ "id": 1 }, { "id": 2 }]
Escaped double quote"She said \"hi\""
Escaped backslash"C:\\path\\file"
Unicode character"\u00E9" (e with accent)
Newline in string"line1\nline2"
Tab in string"col1\tcol2"

Frequently Asked Questions

What is the difference between JSON and a JavaScript object?

A JavaScript object can have unquoted keys, single-quoted strings, functions, undefined values, trailing commas, and comments. JSON allows none of these. JSON is a strict subset of JavaScript object literal syntax, designed for safe data interchange between systems. Every valid JSON document is a valid JavaScript expression, but the reverse is not true.

Can JSON contain comments?

No. The JSON specification does not allow comments of any kind. If you need comments in a configuration file, use JSONC (supported by VS Code and TypeScript configs), JSON5, or YAML instead. Alternatively, you can add a reserved key like "_comment" to store notes, though this adds to the payload size.

Why does my JSON fail to parse even though it looks correct?

The most common causes are trailing commas, single quotes instead of double quotes, unquoted keys, and invisible characters (such as a BOM or non-breaking spaces copied from a website). Paste your JSON into our JSON Formatter to pinpoint the exact line and character where parsing fails.

Is there a maximum nesting depth for JSON?

The specification does not define a maximum depth. In practice, most parsers handle hundreds of levels without issues. However, extremely deep nesting can cause stack overflows in recursive parsers and makes data difficult to work with. Keeping structures under four or five levels deep is a good general practice.

How do I encode binary data in JSON?

JSON has no binary type. The standard approach is to encode your binary data as a Base64 string and store it as a regular JSON string value. The receiving application then decodes the Base64 string back into bytes. For large binary payloads, consider serving the binary data separately and referencing it by URL in your JSON.

Validate Your JSON

When you are unsure whether your JSON is valid, paste it into a formatter and let the tool catch errors for you. Our JSON Formatter validates your input instantly, highlights the exact location of any syntax error, and beautifies the output. Everything runs in your browser with no data sent to a server.

If you are working with JSON as part of a larger workflow, you might also find our guide on how to format JSON online helpful for understanding best practices around indentation, sorting keys, and minification.


Ready to try it yourself?

Open Json Formatter