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 itInput
{
"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.jsonkeys_unsortedCompactTry itInput
{
"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.jsonDoes a key exist? has()
.env | has("PORT"), has("HOST")Try itInput
{
"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
falsejq '.env | has("PORT"), has("HOST")' data.jsonKey/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 itInput
{
"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 itInput
{
"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-1jq -r '.env | to_entries[] | "\(.key)=\(.value)"' data.jsonFilter 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 itInput
{
"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 itInput
{
"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.jsonKeys matching a pattern
with_entries(select(.key | test("^re")))CompactTry itInput
{
"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.jsonCommon mistakes
keyson 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.