Tools
Get Pro

jq: build new objects and pick fields

Curly braces build a new object. It is how you trim a big API response down to the fields you need.

Pick fields: {a, b}

{name, email} is shorthand for {name: .name, email: .email}.

.users[] | {name, email}CompactTry it

Input

{
  "users": [
    {
      "id": 1,
      "name": "Ada",
      "email": "ada@example.com",
      "active": true,
      "country": "UK",
      "tags": [
        "admin",
        "dev"
      ],
      "orders": [
        {
          "id": "o1",
          "total": 30
        },
        {
          "id": "o2",
          "total": 12.5
        }
      ]
    },
    {
      "id": 2,
      "name": "Linus",
      "email": "linus@example.com",
      "active": false,
      "country": "FI",
      "tags": [
        "dev"
      ],
      "orders": []
    },
    {
      "id": 3,
      "name": "Grace",
      "email": "grace@example.com",
      "active": true,
      "country": "US",
      "tags": [
        "dev",
        "ops"
      ],
      "orders": [
        {
          "id": "o3",
          "total": 99
        }
      ]
    }
  ]
}

Output

{"name":"Ada","email":"ada@example.com"}
{"name":"Linus","email":"linus@example.com"}
{"name":"Grace","email":"grace@example.com"}
jq -c '.users[] | {name, email}' data.json

Rename and compute

Write newKey: expression. Wrap anything with a pipe or operator in parentheses.

.users[] | {user: .name, spent: ([.orders[].total] | add // 0)}CompactTry it

Input

{
  "users": [
    {
      "id": 1,
      "name": "Ada",
      "email": "ada@example.com",
      "active": true,
      "country": "UK",
      "tags": [
        "admin",
        "dev"
      ],
      "orders": [
        {
          "id": "o1",
          "total": 30
        },
        {
          "id": "o2",
          "total": 12.5
        }
      ]
    },
    {
      "id": 2,
      "name": "Linus",
      "email": "linus@example.com",
      "active": false,
      "country": "FI",
      "tags": [
        "dev"
      ],
      "orders": []
    },
    {
      "id": 3,
      "name": "Grace",
      "email": "grace@example.com",
      "active": true,
      "country": "US",
      "tags": [
        "dev",
        "ops"
      ],
      "orders": [
        {
          "id": "o3",
          "total": 99
        }
      ]
    }
  ]
}

Output

{"user":"Ada","spent":42.5}
{"user":"Linus","spent":0}
{"user":"Grace","spent":99}
jq -c '.users[] | {user: .name, spent: ([.orders[].total] | add // 0)}' data.json

Pick nested fields

{id, city: .customer.address.city, items: (.items | length)}CompactTry 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

{"id":"A-1001","city":"London","items":2}
jq -c '{id, city: .customer.address.city, items: (.items | length)}' data.json

Computed keys: {(expr): value}

Put an expression in parentheses to use its value as the key.

.users | map({(.name): .country}) | addCompactTry it

Input

{
  "users": [
    {
      "id": 1,
      "name": "Ada",
      "email": "ada@example.com",
      "active": true,
      "country": "UK",
      "tags": [
        "admin",
        "dev"
      ],
      "orders": [
        {
          "id": "o1",
          "total": 30
        },
        {
          "id": "o2",
          "total": 12.5
        }
      ]
    },
    {
      "id": 2,
      "name": "Linus",
      "email": "linus@example.com",
      "active": false,
      "country": "FI",
      "tags": [
        "dev"
      ],
      "orders": []
    },
    {
      "id": 3,
      "name": "Grace",
      "email": "grace@example.com",
      "active": true,
      "country": "US",
      "tags": [
        "dev",
        "ops"
      ],
      "orders": [
        {
          "id": "o3",
          "total": 99
        }
      ]
    }
  ]
}

Output

{"Ada":"UK","Linus":"FI","Grace":"US"}
An array of one-key objects, merged with add, becomes a lookup table. jq -c '.users | map({(.name): .country}) | add' data.json

Merge objects: + and *

+ merges two objects (right side wins). * merges recursively, so nested objects are combined instead of replaced.

{a: 1, b: {x: 1}} + {b: {y: 2}}CompactTry it

Input

null

Output

{"a":1,"b":{"y":2}}
jq -c '{a: 1, b: {x: 1}} + {b: {y: 2}}' data.json
{a: 1, b: {x: 1}} * {b: {y: 2}}CompactTry it

Input

null

Output

{"a":1,"b":{"x":1,"y":2}}
jq -c '{a: 1, b: {x: 1}} * {b: {y: 2}}' data.json

Array to object by id: INDEX()

INDEX(f) turns an array into an object keyed by f — handy for lookups.

.users | INDEX(.id) | map_values(.name)CompactTry it

Input

{
  "users": [
    {
      "id": 1,
      "name": "Ada",
      "email": "ada@example.com",
      "active": true,
      "country": "UK",
      "tags": [
        "admin",
        "dev"
      ],
      "orders": [
        {
          "id": "o1",
          "total": 30
        },
        {
          "id": "o2",
          "total": 12.5
        }
      ]
    },
    {
      "id": 2,
      "name": "Linus",
      "email": "linus@example.com",
      "active": false,
      "country": "FI",
      "tags": [
        "dev"
      ],
      "orders": []
    },
    {
      "id": 3,
      "name": "Grace",
      "email": "grace@example.com",
      "active": true,
      "country": "US",
      "tags": [
        "dev",
        "ops"
      ],
      "orders": [
        {
          "id": "o3",
          "total": 99
        }
      ]
    }
  ]
}

Output

{"1":"Ada","2":"Linus","3":"Grace"}
jq -c '.users | INDEX(.id) | map_values(.name)' data.json

Common mistakes

  • {.name} is a syntax error. Write {name} or {name: .name}.
  • Keys built from numbers must be strings: {(.id | tostring): .name}.

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.