jq cheatsheet

Types & defaults

Handle missing data, types, keys, and entries. Explore 5 examples, then try one against the included input or your own data.

jq examples

5 examplesHandle missing data, types, keys, and entries.
Types & defaults

Provide a fallback value

// returns the right side when the left produces false, null, or no value.

.nickname // "Anonymous"
Input and expected output
Input
{"name":"Ada","nickname":null}
Expected output
"Anonymous"

Boolean false also activates the fallback.

Types & defaults

Inspect value types

type reports jq types such as object, array, number, and string.

map(type)
Input and expected output
Input
[null,true,12,"jq",[],{}]
Expected output
[
  "null",
  "boolean",
  "number",
  "string",
  "array",
  "object"
]
Types & defaults

List object keys

keys returns sorted object keys; keys_unsorted keeps approximate insertion order.

keys
Input and expected output
Input
{"z":1,"a":2,"m":3}
Expected output
[
  "a",
  "m",
  "z"
]
Types & defaults

Rename every object key

with_entries exposes each property as {key, value}, then rebuilds the object.

with_entries(.key |= ascii_downcase)
Input and expected output
Input
{"FirstName":"Ada","ROLE":"Admin"}
Expected output
{
  "firstname": "Ada",
  "role": "Admin"
}
Types & defaults

Remove sensitive fields

del removes one or more paths from a copied result.

del(.password, .token)
Input and expected output
Input
{"name":"Ada","password":"secret","token":"abc"}
Expected output
{
  "name": "Ada"
}