Tools
Get Pro

jq: add, update and delete fields

Everything so far read JSON. jq can also change it and output the whole document with your edits — the update operators keep everything you did not touch. jq never edits the file itself; redirect the output to save it.

Set a value: =

.status = "shipped"CompactTry it

Input

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

Output

{"id":"A-1001","status":"shipped"}
jq -c '.status = "shipped"' data.json

Add a field

Assigning to a key that does not exist adds it — even nested.

.meta.source = "import"CompactTry it

Input

{"id":"A-1001"}

Output

{"id":"A-1001","meta":{"source":"import"}}
jq -c '.meta.source = "import"' data.json

Update based on the old value: |=

|= runs a filter on the current value. Shortcuts: +=, -=, *=, /=.

.replicas |= . * 2CompactTry it

Input

{
  "name": "api",
  "env": {
    "PORT": "8080",
    "DEBUG": "false",
    "REGION": "eu-west-1"
  },
  "replicas": 2,
  "database": {
    "host": "db.internal",
    "password": "secret",
    "pool": {
      "max": 10,
      "password": "hunter2"
    }
  }
}

Output

{"name":"api","env":{"PORT":"8080","DEBUG":"false","REGION":"eu-west-1"},"replicas":4,"database":{"host":"db.internal","password":"secret","pool":{"max":10,"password":"hunter2"}}}
jq -c '.replicas |= . * 2' data.json
.items[].qty += 1 | .itemsCompactTry 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

[{"sku":"pen","qty":4,"price":1.5},{"sku":"notebook","qty":2,"price":4}]
Every item gets one more. jq -c '.items[].qty += 1 | .items' data.json

Update matching items only

Put a select in the path on the left.

(.users[] | select(.id == 2) | .country) = "SE" | .users | map({id, country})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

[{"id":1,"country":"UK"},{"id":2,"country":"SE"},{"id":3,"country":"US"}]
Only user 2 changed; the rest of the document is kept. jq -c '(.users[] | select(.id == 2) | .country) = "SE" | .users | map({id, country})' data.json

Delete keys: del()

del(.database.password)CompactTry it

Input

{
  "name": "api",
  "env": {
    "PORT": "8080",
    "DEBUG": "false",
    "REGION": "eu-west-1"
  },
  "replicas": 2,
  "database": {
    "host": "db.internal",
    "password": "secret",
    "pool": {
      "max": 10,
      "password": "hunter2"
    }
  }
}

Output

{"name":"api","env":{"PORT":"8080","DEBUG":"false","REGION":"eu-west-1"},"replicas":2,"database":{"host":"db.internal","pool":{"max":10,"password":"hunter2"}}}
jq -c 'del(.database.password)' data.json
.users | map(del(.orders, .tags))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

[{"id":1,"name":"Ada","email":"ada@example.com","active":true,"country":"UK"},{"id":2,"name":"Linus","email":"linus@example.com","active":false,"country":"FI"},{"id":3,"name":"Grace","email":"grace@example.com","active":true,"country":"US"}]
jq -c '.users | map(del(.orders, .tags))' data.json

Redact a key at any depth

.. visits every value; combine it with an update to change every matching key.

(.. | objects | select(has("password")) | .password) = "***"CompactTry it

Input

{
  "name": "api",
  "env": {
    "PORT": "8080",
    "DEBUG": "false",
    "REGION": "eu-west-1"
  },
  "replicas": 2,
  "database": {
    "host": "db.internal",
    "password": "secret",
    "pool": {
      "max": 10,
      "password": "hunter2"
    }
  }
}

Output

{"name":"api","env":{"PORT":"8080","DEBUG":"false","REGION":"eu-west-1"},"replicas":2,"database":{"host":"db.internal","password":"***","pool":{"max":10,"password":"***"}}}
jq -c '(.. | objects | select(has("password")) | .password) = "***"' data.json

Save the result

jq writes to standard output. Use a temporary file — redirecting straight back to the input empties it:

jq '.replicas = 3' config.json > tmp.json && mv tmp.json config.json

Common mistakes

  • .a = .b and .a |= .b differ: = evaluates the right side against the whole input, |= against the old value of .a.
  • jq … file.json > file.json truncates the file before jq reads it.

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.