Tools
Get Pro

jq basics: get fields and nested values

jq is a small language for reading and reshaping JSON. A jq program is a filter: it takes JSON in and sends JSON out. The simplest filters just walk into the document and pick a value out of it. Every example below runs in your browser — press Try it to open it in the jq Playground and change it.

The identity filter: .

. outputs its input unchanged. It is useful on its own to pretty-print JSON (jq . file.json), and it is where every path starts.

Input

{"id":"A-1001","status":"paid","total":8.5}

Output

{
  "id": "A-1001",
  "status": "paid",
  "total": 8.5
}
jq '.' data.json

Get a field: .key

Write a dot and the key name. Chain keys to go deeper — .customer.address.city is the same as .customer | .address | .city.

.statusTry it

Input

{
  "id": "A-1001",
  "customer": {
    "name": "Ada Lovelace",
    "address": {
      "city": "London",
      "zip": "NW1"
    }
  },
  "items": [
    {
      "sku": "pen",
      "qty": 3,
      "price": 1.5
    },
    {
      "sku": "notebook",
      "qty": 1,
      "price": 4
    }
  ],
  "status": "paid",
  "created at": "2026-11-01T10:00:00Z"
}

Output

"paid"
jq '.status' data.json
.customer.address.cityTry it

Input

{
  "id": "A-1001",
  "customer": {
    "name": "Ada Lovelace",
    "address": {
      "city": "London",
      "zip": "NW1"
    }
  },
  "items": [
    {
      "sku": "pen",
      "qty": 3,
      "price": 1.5
    },
    {
      "sku": "notebook",
      "qty": 1,
      "price": 4
    }
  ],
  "status": "paid",
  "created at": "2026-11-01T10:00:00Z"
}

Output

"London"
Each dot steps one level deeper. jq '.customer.address.city' data.json

Keys with spaces or dashes: ."key" and .["key"]

Keys that are not plain identifiers (spaces, dashes, leading digits) need quotes: ."created at" or .["created at"].

."created at"Try it

Input

{
  "id": "A-1001",
  "customer": {
    "name": "Ada Lovelace",
    "address": {
      "city": "London",
      "zip": "NW1"
    }
  },
  "items": [
    {
      "sku": "pen",
      "qty": 3,
      "price": 1.5
    },
    {
      "sku": "notebook",
      "qty": 1,
      "price": 4
    }
  ],
  "status": "paid",
  "created at": "2026-11-01T10:00:00Z"
}

Output

"2026-11-01T10:00:00Z"
jq '."created at"' data.json

Missing keys give null

Asking for a key that does not exist is not an error — you get null. Use the alternative operator // to supply a default.

.customer.phoneTry it

Input

{
  "id": "A-1001",
  "customer": {
    "name": "Ada Lovelace",
    "address": {
      "city": "London",
      "zip": "NW1"
    }
  },
  "items": [
    {
      "sku": "pen",
      "qty": 3,
      "price": 1.5
    },
    {
      "sku": "notebook",
      "qty": 1,
      "price": 4
    }
  ],
  "status": "paid",
  "created at": "2026-11-01T10:00:00Z"
}

Output

null
jq '.customer.phone' data.json
.customer.phone // "no phone"Try it

Input

{
  "id": "A-1001",
  "customer": {
    "name": "Ada Lovelace",
    "address": {
      "city": "London",
      "zip": "NW1"
    }
  },
  "items": [
    {
      "sku": "pen",
      "qty": 3,
      "price": 1.5
    },
    {
      "sku": "notebook",
      "qty": 1,
      "price": 4
    }
  ],
  "status": "paid",
  "created at": "2026-11-01T10:00:00Z"
}

Output

"no phone"
a // b gives b when a is null or false. jq '.customer.phone // "no phone"' data.json

Avoid errors with ?

Indexing a string or number with .key is an error. Add ? to skip values where the path does not make sense.

.status.code?Try it

Input

{
  "id": "A-1001",
  "customer": {
    "name": "Ada Lovelace",
    "address": {
      "city": "London",
      "zip": "NW1"
    }
  },
  "items": [
    {
      "sku": "pen",
      "qty": 3,
      "price": 1.5
    },
    {
      "sku": "notebook",
      "qty": 1,
      "price": 4
    }
  ],
  "status": "paid",
  "created at": "2026-11-01T10:00:00Z"
}

Output

(no output)
No output and no error: the ? swallowed it. jq '.status.code?' data.json

Common mistakes

  • Forgetting the leading dot: status calls a function named status; .status reads the field.
  • Using shell double quotes around the filter: write jq '.status' on the command line so the shell leaves it alone.

Practice

Open the jq Playground with your own JSON — it runs the real jq 1.8 in your browser, so nothing is uploaded. The jq cheat sheet lists every filter from these lessons on one page.