Tools
Get Pro

jq recursive search: find a key at any depth

When you do not know where a value sits — or it appears at many levels — recursive descent finds it.

Every value: ..

.. outputs the input and every value nested inside it, at any depth.

[.. | numbers]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

[2,10]
jq -c '[.. | numbers]' data.json

Every value of a key, at any depth

.password? reads the key where it exists; // empty drops the nulls.

[.. | .password? // empty]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

["secret","hunter2"]
jq -c '[.. | .password? // empty]' data.json
[.. | objects | select(has("total")) | .total]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

[30,12.5,99]
Same idea, written with objects + has(). jq -c '[.. | objects | select(has("total")) | .total]' data.json

Where is it? paths and getpath

paths(f) lists the path to every value matching f, as an array of keys and indexes.

[paths(..) | select(.[-1] == "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

[["database","password"],["database","pool","password"]]
jq -c '[paths(..) | select(.[-1] == "password")]' data.json
getpath(["database","pool","max"])Try 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

10
jq 'getpath(["database","pool","max"])' data.json

Flatten to dotted keys

Turn a nested object into {"a.b.c": value} pairs — handy for diffs and spreadsheets.

[paths(scalars) as $p | {($p | map(tostring) | join(".")): getpath($p)}] | addCompactTry 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","env.DEBUG":"false","env.REGION":"eu-west-1","replicas":2,"database.host":"db.internal","database.password":"secret","database.pool.max":10,"database.pool.password":"hunter2"}
jq -c '[paths(scalars) as $p | {($p | map(tostring) | join(".")): getpath($p)}] | add' data.json

Common mistakes

  • .. | .key without ? fails on the first string or number it meets.

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.