Tools
Get Pro

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 it

Input

{
  "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 US
jq -r '.users[] | "\(.name) lives in \(.country)"' data.json

contains, startswith, endswith

.users[] | select(.email | endswith("@example.com")) | .nameTry it

Input

{
  "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.json

Regex: 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 it

Input

{"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
5011
The input is NDJSON, so the filter runs once per line. jq 'select(.msg | test("TIMEOUT"; "i")) | .ms' data.json
."created at" | capture("(?<date>[0-9-]+)T(?<time>[0-9:]+)")CompactTry it

Input

{
  "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.json

split and join

.env.REGION | split("-")CompactTry it

Input

{
  "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 it

Input

{"tags":["dev","ops","admin"]}

Output

dev, ops, admin
jq -r '.tags | join(", ")' data.json

Replace: sub and gsub

sub replaces the first match, gsub every match.

.customer.name | gsub("[aeiou]"; "*")Try it

Input

{
  "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.json

Case, trim and length

" Hello JQ " | trim | ascii_downcase, lengthTry it

Input

null

Output

"hello jq"
8
jq '" Hello JQ " | trim | ascii_downcase, length' data.json

Numbers and strings

Convert with tostring and tonumber. + does not mix types.

"Order " + (.items | length | tostring) + " items"Try it

Input

{
  "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.json

Common mistakes

  • Regex backslashes need escaping in the JSON string: test("\\d+").
  • "a" + 1 is an error. Use interpolation or tostring.

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.