Tools
Get Pro

jq keys, to_entries and with_entries

Objects need different tools than arrays. These turn an object into something you can iterate, filter and rebuild.

List keys: keys and keys_unsorted

keys sorts them; keys_unsorted keeps document order.

.env | keysCompactTry 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

["DEBUG","PORT","REGION"]
jq -c '.env | keys' data.json
keys_unsortedCompactTry 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","env","replicas","database"]
jq -c 'keys_unsorted' data.json

Does a key exist? has()

.env | has("PORT"), has("HOST")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

true
false
jq '.env | has("PORT"), has("HOST")' data.json

Key/value pairs: to_entries

to_entries turns {"k": v} into [{"key": "k", "value": v}], so you can use array filters on it.

.env | to_entriesCompactTry 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

[{"key":"PORT","value":"8080"},{"key":"DEBUG","value":"false"},{"key":"REGION","value":"eu-west-1"}]
jq -c '.env | to_entries' data.json
.env | to_entries[] | "\(.key)=\(.value)"Raw outputTry 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

PORT=8080
DEBUG=false
REGION=eu-west-1
Print an .env file. jq -r '.env | to_entries[] | "\(.key)=\(.value)"' data.json

Filter or rename keys: with_entries

with_entries(f) is to_entries | map(f) | from_entries. Inside, change .key or .value, or drop pairs with select.

.env | with_entries(select(.key != "DEBUG"))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

{"PORT":"8080","REGION":"eu-west-1"}
jq -c '.env | with_entries(select(.key != "DEBUG"))' data.json
.env | with_entries(.key |= ascii_downcase)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

{"port":"8080","debug":"false","region":"eu-west-1"}
jq -c '.env | with_entries(.key |= ascii_downcase)' data.json

Keys matching a pattern

with_entries(select(.key | test("^re")))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

{"replicas":2}
jq -c 'with_entries(select(.key | test("^re")))' data.json

Common mistakes

  • keys on an array returns its indexes ([0,1,2]), not the keys of the objects inside.

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.