Skip to content

Instantly share code, notes, and snippets.

@ggorlen
Last active June 3, 2025 16:20
Show Gist options
  • Select an option

  • Save ggorlen/3cacacd650d54b075ea6d9953f86a11f to your computer and use it in GitHub Desktop.

Select an option

Save ggorlen/3cacacd650d54b075ea6d9953f86a11f to your computer and use it in GitHub Desktop.
jq cheatsheet

JQ Cheatsheet

Sample JSON 1:

{
  "data": [
    {
      "bar": "blah",
      "quux": "garply",
      "foo": [
        {
          "x": 3,
          "y": 1,
          "z": 1
        },
        {
          "x": 5,
          "y": 3,
          "z": 1
        }
      ]
    },
    {
      "bar": "asdf",
      "quux": "corge",
      "foo": [
        {
          "x": 2,
          "y": 3,
          "z": 1
        }
      ]
    },
    {
      "bar": "baz",
      "quux": "garply",
      "foo": [
        {
          "x": 5,
          "y": 2,
          "z": 1
        },
        {
          "x": 3,
          "y": 1,
          "z": 1
        }
      ]
    },
    {
      "bar": "hello",
      "quux": "corge"
    }
  ]
}

Patterns and idioms

Filtering parent objects on properties of child arrays

# sample json 1
[
  .data[]
  | select(.foo != null)
  | {
    bar,
    foo: [.foo[] | {x, y}] | sort_by(.x)
       # ^               ^   (the brackets are critical)
  }
  | select(any(.foo[]; .x > 3 and .y > 2)) # <-- (any is critical)
]
| sort_by(.bar)

Manipulating child arrays of property

# sample json 1
.data |= map(select(.quux == "garply"))

Merging counts by key in child array

# sample json 1
# https://stackoverflow.com/questions/28484534/how-do-i-sum-the-values-in-an-array-of-maps-in-jq
[
  .data[].foo
  | select(. != null)
  | map(to_entries)
  | flatten 
  | reduce .[] as $e ({}; .[$e.key] += $e.value)
]
| map(to_entries)
| add
| group_by(.key) # <-- important
| map({key: .[0].key, value: map(.value) | add})
| sort_by(-.value)
| from_entries

Attach parent property to every nested child object and flatten

[.data[] as $parent | $parent.foo[]? | . + {bar: $parent.bar}]

String comparison/searching

def str_case_eq(s): (. | ascii_upcase) == (s | ascii_upcase);
def str_case_contains(s): (. | ascii_upcase) | index(s | ascii_upcase); # see also 'contains' instead of 'index'

"hello" as $foo
| [
  ($foo | str_case_eq("HEllO")),       # true
  ($foo | str_case_eq("EllO")),        # false
  ($foo | str_case_contains("EllO")),  # 1 (0 is also truthy!)
  ($foo | str_case_contains("zEllO"))  # null 
]

Truncating all string fields

jq 'walk(if type == "string" then .[:50] else . end)' foo.json

Convert array of objects to CSV

[
  {
    "accountId": "x",
    "accountNumber": "y",
    "nickname": "z"
  },
  {
    "accountId": "xx",
    "accountNumber": "yy",
    "nickname": "zz"
  }
]
jq -r '(["accountId", "accountNumber", "nickname"], (.[] | [.accountId, .accountNumber, .nickname])) | @csv' input.json

Or generalize with this answer

Big dump of unorganized links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment