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.jsonGet 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 itInput
{
"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 itInput
{
"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"jq '.customer.address.city' data.jsonKeys 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 itInput
{
"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.jsonMissing 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 itInput
{
"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
nulljq '.customer.phone' data.json.customer.phone // "no phone"Try itInput
{
"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.jsonAvoid 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 itInput
{
"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)jq '.status.code?' data.jsonCommon mistakes
- Forgetting the leading dot:
statuscalls a function named status;.statusreads 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.