jq strings: test, contains, split and interpolation
jq has a solid set of string functions, including full regular expressions (PCRE-like, via Oniguruma).
Interpolation: "\(expr)"
.users[] | "\(.name) lives in \(.country)"Raw outputTry itInput
{
"users": [
{
"id": 1,
"name": "Ada",
"email": "ada@example.com",
"active": true,
"country": "UK",
"tags": [
"admin",
"dev"
],
"orders": [
{
"id": "o1",
"total": 30
},
{
"id": "o2",
"total": 12.5
}
]
},
{
"id": 2,
"name": "Linus",
"email": "linus@example.com",
"active": false,
"country": "FI",
"tags": [
"dev"
],
"orders": []
},
{
"id": 3,
"name": "Grace",
"email": "grace@example.com",
"active": true,
"country": "US",
"tags": [
"dev",
"ops"
],
"orders": [
{
"id": "o3",
"total": 99
}
]
}
]
}Output
Ada lives in UK
Linus lives in FI
Grace lives in USjq -r '.users[] | "\(.name) lives in \(.country)"' data.jsoncontains, startswith, endswith
.users[] | select(.email | endswith("@example.com")) | .nameTry itInput
{
"users": [
{
"id": 1,
"name": "Ada",
"email": "ada@example.com",
"active": true,
"country": "UK",
"tags": [
"admin",
"dev"
],
"orders": [
{
"id": "o1",
"total": 30
},
{
"id": "o2",
"total": 12.5
}
]
},
{
"id": 2,
"name": "Linus",
"email": "linus@example.com",
"active": false,
"country": "FI",
"tags": [
"dev"
],
"orders": []
},
{
"id": 3,
"name": "Grace",
"email": "grace@example.com",
"active": true,
"country": "US",
"tags": [
"dev",
"ops"
],
"orders": [
{
"id": "o3",
"total": 99
}
]
}
]
}Output
"Ada"
"Linus"
"Grace"jq '.users[] | select(.email | endswith("@example.com")) | .name' data.jsonRegex: test, match and capture
test(re) returns true/false. capture(re) returns named groups as an object. Add "i" as a second argument for case-insensitive matching.
select(.msg | test("TIMEOUT"; "i")) | .msTry itInput
{"level":"info","msg":"started","ms":12}
{"level":"error","msg":"db timeout","ms":5003}
{"level":"info","msg":"request","ms":48}
{"level":"error","msg":"db timeout","ms":5011}Output
5003
5011jq 'select(.msg | test("TIMEOUT"; "i")) | .ms' data.json."created at" | capture("(?<date>[0-9-]+)T(?<time>[0-9:]+)")CompactTry itInput
{
"id": "A-1001",
"customer": {
"name": "Ada Lovelace",
"address": {
"city": "London",
"zip": "NW1"
}
},
"items": [
{
"sku": "pen",
"qty": 3,
"price": 1.5
},
{
"sku": "notebook",
"qty": 1,
"price": 4
}
],
"status": "paid",
"created at": "2026-11-01T10:00:00Z"
}Output
{"date":"2026-11-01","time":"10:00:00"}jq -c '."created at" | capture("(?<date>[0-9-]+)T(?<time>[0-9:]+)")' data.jsonsplit and join
.env.REGION | split("-")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
["eu","west","1"]jq -c '.env.REGION | split("-")' data.json.tags | join(", ")Raw outputTry itInput
{"tags":["dev","ops","admin"]}Output
dev, ops, adminjq -r '.tags | join(", ")' data.jsonReplace: sub and gsub
sub replaces the first match, gsub every match.
.customer.name | gsub("[aeiou]"; "*")Try itInput
{
"id": "A-1001",
"customer": {
"name": "Ada Lovelace",
"address": {
"city": "London",
"zip": "NW1"
}
},
"items": [
{
"sku": "pen",
"qty": 3,
"price": 1.5
},
{
"sku": "notebook",
"qty": 1,
"price": 4
}
],
"status": "paid",
"created at": "2026-11-01T10:00:00Z"
}Output
"Ad* L*v*l*c*"jq '.customer.name | gsub("[aeiou]"; "*")' data.jsonCase, trim and length
" Hello JQ " | trim | ascii_downcase, lengthTry itInput
nullOutput
"hello jq"
8jq '" Hello JQ " | trim | ascii_downcase, length' data.jsonNumbers and strings
Convert with tostring and tonumber. + does not mix types.
"Order " + (.items | length | tostring) + " items"Try itInput
{
"id": "A-1001",
"customer": {
"name": "Ada Lovelace",
"address": {
"city": "London",
"zip": "NW1"
}
},
"items": [
{
"sku": "pen",
"qty": 3,
"price": 1.5
},
{
"sku": "notebook",
"qty": 1,
"price": 4
}
],
"status": "paid",
"created at": "2026-11-01T10:00:00Z"
}Output
"Order 2 items"jq '"Order " + (.items | length | tostring) + " items"' data.jsonCommon mistakes
- Regex backslashes need escaping in the JSON string:
test("\\d+"). "a" + 1is an error. Use interpolation ortostring.
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.