jq starts with query

If a user wants to search a json payload using a starts_with or “begins with” or “has prefix” query, jq natively supports it.

How to query using startswith

Given a json array, searching for a specific string that startswith a prefix is easy.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
  "people": [
    {
      "firstName": "thomas",
      "age": 25
    },
    {
      "firstName": "sally",
      "age": 28
    }
  ]
}

Find each person if their name begins with sal.

1
jq -r '.people[]|select(.firstName | startswith("sal"))' people.json

Output

1
2
3
4
{
  "firstName": "sally",
  "age": 28
}