jq cheatsheet

Strings & regex

Split, join, match, capture, and replace text. Explore 4 examples, then try one against the included input or your own data.

jq examples

4 examplesSplit, join, match, capture, and replace text.
Strings & regex

Split and join text

split creates an array; join combines its values with a separator.

.path | split("/") | map(ascii_upcase) | join(" › ")
Input and expected output
Input
{"path":"docs/guides/jq"}
Expected output
"DOCS › GUIDES › JQ"
Strings & regex

Test a regular expression

test returns a boolean and accepts optional regex flags.

.users[] | select(.email | test("@example\.com$"; "i")) | .name
Input and expected output
Input
{"users":[{"name":"Ada","email":"ada@EXAMPLE.com"},{"name":"Grace","email":"grace@test.dev"}]}
Expected output
"Ada"
Strings & regex

Capture named regex groups

capture returns an object whose keys are the named groups.

capture("(?<major>[0-9]+)\.(?<minor>[0-9]+)")
Input and expected output
Input
"version 1.8"
Expected output
{
  "major": "1",
  "minor": "8"
}
Strings & regex

Replace every text match

gsub replaces all non-overlapping regex matches.

gsub("[^a-zA-Z0-9]+"; "-") | ascii_downcase
Input and expected output
Input
"Hello, jq world!"
Expected output
"hello-jq-world-"