jq cheatsheet

Construct values

Build new objects, strings, and updated structures. Explore 4 examples, then try one against the included input or your own data.

jq examples

4 examplesBuild new objects, strings, and updated structures.
Construct values

Reshape an object

Build a new object and use shorthand when the key and field name match.

{id, displayName: .profile.name}
Input and expected output
Input
{"id":7,"profile":{"name":"Ada"},"private":true}
Expected output
{
  "id": 7,
  "displayName": "Ada"
}
Construct values

Create a computed object key

Put an expression in parentheses to compute an object key.

{(.key): .value}
Input and expected output
Input
{"key":"status","value":"ready"}
Expected output
{
  "status": "ready"
}
Construct values

Interpolate values into a string

\(filter) inserts a filter result into a jq string.

.users[] | "\(.name): \(.role)"
Input and expected output
Input
{"users":[{"name":"Ada","role":"admin"}]}
Expected output
"Ada: admin"
Construct values

Update nested data

|= feeds the current value through a filter and writes the result back.

.users |= map(. + {active: true})
Input and expected output
Input
{"users":[{"name":"Ada"},{"name":"Grace"}]}
Expected output
{
  "users": [
    {
      "name": "Ada",
      "active": true
    },
    {
      "name": "Grace",
      "active": true
    }
  ]
}