JSON Objects and Arrays

Patterns for nested objects, arrays, list items, nullability, and common data-shape decisions in JSON.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Object patterns
Simple object
{
  "id": 101,
  "name": "Ada"
}

# Use objects for named properties.

Nested object
{
  "user": {
    "id": 101,
    "profile": {
      "city": "Austin",
      "timezone": "America/Chicago"
    }
  }
}

# Represent related subfields in a child object.

Missing property vs null property
{
  "nickname": null
}

# Omitting a field is different from setting it to null.

## Array patterns
Array of strings
[
  "red",
  "green",
  "blue"
]

# A simple list of primitive values.

Array of objects
[
  { "id": 1, "name": "Ada" },
  { "id": 2, "name": "Linus" }
]

# A common shape for API collections.

Mixed-type arrays are legal but risky
[1, "two", true, null]

# JSON allows mixed types, but they are often harder to consume.

Extract a field from each array item
jq '.[].name' users.json

# Print one property from every object in an array.

Recommended next

No recommendations yet.