JSON Syntax Explained: Objects, Arrays, and Data Types

ยท 10 min read

So you know JSON exists and you've probably copy pasted some from Stack Overflow. But do you actually know the rules? Most developers learn JSON through trial and error, getting parse errors until something works.

Let's fix that. This guide covers everything about JSON syntax so you can write it correctly the first time.

The Basics: Two Structures

JSON has exactly two structural types: objects and arrays. That's it. Every valid JSON document is either an object, an array, or one of the primitive values (which we'll cover later).

An object is a collection of key value pairs wrapped in curly braces {}. Think of it like a dictionary or a map.

An array is an ordered list of values wrapped in square brackets []. Like a list in Python or an array in JavaScript.

Here's the simplest possible examples of each:

// Object
{"name": "Alice"}

// Array
["apple", "banana", "cherry"]

Most real world JSON combines both. You'll have objects containing arrays, arrays containing objects, objects within objects, and so on.

Objects in Depth

Objects are probably what you'll work with most. Here's the formal syntax, if you're into that:

{
  "key1": value1,
  "key2": value2,
  "key3": value3
}

Rules for keys

  • Keys must be strings. Always.
  • Keys must be wrapped in double quotes. Single quotes don't work.
  • Keys should be unique within an object (duplicates cause unpredictable behavior)
  • Keys can contain spaces, but it's generally a bad idea

Rules for values

Values can be any of the six JSON data types: string, number, boolean, null, object, or array. We'll cover each below.

Punctuation matters

  • Key value pairs are separated by a colon :
  • Multiple pairs are separated by commas
  • No comma after the last pair (this trips people up constantly)
{
  "id": 12345,
  "name": "Bob Smith",
  "active": true,
  "email": null
}

Empty objects are valid

Just {} on its own is perfectly valid JSON. You'll see this in API responses when there's no data to return, or as a default value.

Arrays in Depth

Arrays hold ordered lists of values. The order is preserved, which matters when you're parsing the data.

[value1, value2, value3]

Rules for arrays

  • Values are separated by commas
  • No comma after the last value
  • Values can be any JSON data type, including other arrays or objects
  • Values don't have to be the same type (but usually are in practice)
// Array of strings
["red", "green", "blue"]

// Array of numbers
[1, 2, 3, 4, 5]

// Array of objects (very common in APIs)
[
  {"id": 1, "name": "Alice"},
  {"id": 2, "name": "Bob"},
  {"id": 3, "name": "Charlie"}
]

// Mixed types (valid but unusual)
[42, "hello", true, null]

Empty arrays are valid

Just [] is fine. You'll see this when an API returns a list with no items, or when initializing a collection.

The Six Data Types

JSON supports exactly six data types. No more, no less. If you need something else (like a date), you have to represent it using one of these.

1. Strings

Text wrapped in double quotes. Must be double quotes.

"Hello, world!"
"user@example.com"
"123 Main St"
""

Strings can contain escape sequences:

  • \" for a double quote
  • \\ for a backslash
  • \n for a newline
  • \t for a tab
  • \uXXXX for Unicode characters

2. Numbers

Integers or decimals. No quotes. JSON doesn't distinguish between int and float, that's up to your parser.

42
3.14159
-17
1.5e10
0

What you can't do: leading zeros (except for 0 itself), hex notation, NaN, or Infinity. Those aren't valid JSON numbers.

3. Booleans

Either true or false. Lowercase, no quotes.

true
false

4. Null

Represents "nothing" or "no value". Lowercase, no quotes.

null

Note: JSON has no undefined. That's a JavaScript thing. If a value doesn't exist, you either use null or omit the key entirely.

5. Objects

Key value collections as described above. Values can be any type.

{
  "nested": {
    "deeper": {
      "value": 42
    }
  }
}

6. Arrays

Ordered lists as described above.

[1, [2, 3], [[4, 5], [6, 7]]]

What JSON doesn't support

  • Dates: Store as ISO 8601 strings like "2026-01-28T10:30:00Z"
  • Functions: Not possible, JSON is data only
  • undefined: Use null instead
  • Comments: Nope, not supported
  • Binary data: Encode as Base64 strings

Nesting and Complex Structures

Real world JSON is almost always nested. Objects contain arrays, arrays contain objects, and it can go many levels deep. Here's a realistic example:

{
  "user": {
    "id": 12345,
    "profile": {
      "name": "Alice Johnson",
      "email": "alice@example.com",
      "avatar": null
    },
    "roles": ["admin", "editor"],
    "preferences": {
      "theme": "dark",
      "notifications": {
        "email": true,
        "push": false
      }
    }
  },
  "meta": {
    "api_version": "2.0",
    "timestamp": "2026-01-28T10:30:00Z"
  }
}

There's no limit to how deep you can nest, but deeply nested JSON becomes hard to work with. If you're going more than 3 or 4 levels deep, consider flattening your structure.

Arrays of objects

This is probably the most common pattern you'll see in APIs. A list of similar items, each represented as an object:

{
  "products": [
    {
      "id": 1,
      "name": "Widget",
      "price": 9.99,
      "inStock": true
    },
    {
      "id": 2,
      "name": "Gadget",
      "price": 19.99,
      "inStock": false
    }
  ],
  "total": 2
}

Whitespace and Formatting

Whitespace (spaces, tabs, newlines) is ignored between tokens. These two are equivalent:

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

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

The minified version is smaller (good for network transfer), but the formatted version is readable (good for debugging). Most tools can convert between the two.

Whitespace inside strings is preserved though. "hello world" is different from "helloworld".

Common Gotchas

These are the things that'll break your JSON and give you cryptic error messages:

Trailing commas

JavaScript allows them. JSON does not.

// WRONG
{
  "name": "Alice",
  "age": 28,
}

// RIGHT
{
  "name": "Alice",
  "age": 28
}

Single quotes

JSON requires double quotes for strings and keys.

// WRONG
{'name': 'Alice'}

// RIGHT
{"name": "Alice"}

Unquoted keys

JavaScript objects allow unquoted keys. JSON doesn't.

// WRONG
{name: "Alice"}

// RIGHT
{"name": "Alice"}

Comments

You cannot add comments to JSON. If you need them, use a format like JSONC (JSON with Comments) or YAML instead.

Leading zeros

Numbers can't have leading zeros (except for 0 itself and decimals like 0.5).

// WRONG
{"code": 007}

// RIGHT
{"code": 7}
// Or use a string if you need the format
{"code": "007"}

Having trouble with JSON errors? Try our JSON cleaner tool to validate and fix common issues.