If you tacked the following filter onto the one you already have, then you'd get the output shown below:

map(select(.Address | test("^[0-9]")))

Output:

[
  {
    "Address": "1 Bridge Rd"
  }
]

For robustness, you might like to consider adding ? after the test:

map(select(.Address | test("^[0-9]")?))

Or, you could combine the two calls to map in various ways. You might like to consider:

.[].payload.address | select(test("^[0-9]")?) | {Address: .}
Answer from peak on Stack Overflow
Top answer
1 of 2
60

If you tacked the following filter onto the one you already have, then you'd get the output shown below:

map(select(.Address | test("^[0-9]")))

Output:

[
  {
    "Address": "1 Bridge Rd"
  }
]

For robustness, you might like to consider adding ? after the test:

map(select(.Address | test("^[0-9]")?))

Or, you could combine the two calls to map in various ways. You might like to consider:

.[].payload.address | select(test("^[0-9]")?) | {Address: .}
2 of 2
10

Though the accepted answer works, I find this more readable

 echo '[{"Address": "The Sq", "n": 1}, {"Address": "1 Bridge Rd", "n": 2}]' | \
  jq '.[] | .Address | select(.|test("^[0-9]"))'

"1 Bridge Rd"
 echo '[{"Address": "The Sq", "n": 1}, {"Address": "1 Bridge Rd", "n": 2}]' | \
  jq '.[] | select(.Address|test("^[0-9]"))'

{
  "Address": "1 Bridge Rd",
  "n": 2
}
$

If you want to do a little different filtering:

$ echo '[{"name": "john doe", "sex": "male", "age": 26, "occupation": "city planner", "cod": "asphyxiation"}, {"name": "jane doe", "sex": "male", "age": 24, "occupation": "beautician", "cod": "strangulation"}, {"name": "crispy lips", "sex": "male", "age": 38, "occupation": "convicted killer"} ]' > in.json
$ cat in.json | jq .
[
  {
    "name": "john doe",
    "sex": "male",
    "age": 26,
    "occupation": "city planner",
    "cod": "asphyxiation"
  },
  {
    "name": "jane doe",
    "sex": "male",
    "age": 24,
    "occupation": "beautician",
    "cod": "strangulation"
  },
  {
    "name": "crispy lips",
    "sex": "male",
    "age": 38,
    "occupation": "convicted killer"
  }
]
$

Then we can do basic regex-filter/transform like this:

$ cat in.json | jq '.[] | .name'
"john doe"
"jane doe"
"crispy lips"
 cat in.json | jq '.[] | .name | select(.|test(".*doe"))'
"john doe"
"jane doe"
 cat in.json | jq '.[] | select(.name|test(".*doe"))'
{
  "name": "john doe",
  "sex": "male",
  "age": 26,
  "occupation": "city planner",
  "cod": "asphyxiation"
}
{
  "name": "jane doe",
  "sex": "male",
  "age": 24,
  "occupation": "beautician",
  "cod": "strangulation"
}
 cat in.json | jq '.[] | select(.name|test(".*doe")) | {n: .name, a: .age}'
{
  "n": "john doe",
  "a": 26
}
{
  "n": "jane doe",
  "a": 24
}
 
🌐
Exercism
exercism.org › tracks › jq › concepts › regular-expressions
Regular Expressions in jq on Exercism
Regular expressions in jq are limited to a set of filters. When you need to know if a string matches a pattern, use the test filter. STRING | test(REGEX) STRING | test(REGEX; FLAGS) STRING | test([REGEX, FLAGS])
🌐
jq
jqlang.org › manual
jq 1.8 Manual
Emit a stream of the non-overlapping substrings of the input that match the regex in accordance with the flags, if any have been specified. If there is no match, the stream is empty.
🌐
Today I Learned
til.hashrocket.com › posts › uv0bjiokwk-use-jq-to-filter-objects-list-with-regex
Use jq to filter objects list with regex - Today I Learned
November 21, 2023 - > data | jq '.[] | select(true) | select(.name)' {name: 'Chris', id: 'aabbcc'} {name: 'Ryan', id: 'ddeeff'} {name: 'Ifu', id: 'aaddgg'} Then use the test function to compare an attribute to a regex.
🌐
DevTools Daily
devtoolsdaily.com › jq › regex
Using Regular Expressions in JQ
Here we will explore how to use regular expressions in JQ with functions like test, match, split, and sub.
🌐
Ubuntu Manpages
manpages.ubuntu.com › manpages › focal › man1 › jq.1.html
Ubuntu Manpage: jq - Command-line JSON processor
FLAGS is a string consisting of one of more of the supported flags: ○ g - Global search (find all matches, not just the first) ○ i - Case insensitive search ○ m - Multi line mode (´.´ will match newlines) ○ n - Ignore empty matches ○ p - Both s and m modes are enabled ○ s - Single line mode (´^´ -> ´\A´, ´$´ -> ´\Z´) ○ l - Find longest possible matches ○ x - Extended regex format (ignore whitespace and comments) To match whitespace in an x pattern use an escape such as \s, e.g. ○ test( "a\sb", "x" ). Note that certain flags may also be specified within REGEX, e.g. ○ jq -n ´("test", "TEst", "teST", "TEST") | test( "(?i)te(?-i)st" )´ evaluates to: true, true, false, false.
🌐
Zendesk Developer Docs
developer.zendesk.com › documentation › integration-services › developer-guide › jq-cheat-sheet
jq cheat sheet | Zendesk Developer Docs
Use the match filter to extract a pattern from a string using a regular expression (regex). For supported regex syntax, refer to the re2 documentation.
🌐
Exercism
exercism.org › tracks › jq › exercises › regular-chatbot
Regular Chatbot in jq on Exercism
Regular expressions in jq are limited to a set of filters. When you need to know if a string matches a pattern, use the test filter. STRING | test(REGEX) STRING | test(REGEX; FLAGS) STRING | test([REGEX, FLAGS])
Find elsewhere
🌐
GitHub
github.com › jqlang › jq › issues › 1052
Conditional statement with regular expression · Issue #1052 · jqlang/jq
December 23, 2015 - azure group list --json | jq '.[].name | if test("Acad") then .
Author   pcgeek86
🌐
Mark Biek
mark.biek.org › 2021 › 11 › using-jq-to-filter-json-by-regex
Using jq to filter JSON by regex – Mark Biek
November 17, 2021 - It’s super easy to get all of the domain objects using the jq filter `.domains[]` and it’s also trivial to get at only specific fields of each domain object (eg `.domains[] | {id,name}`). Now let’s say we want to only get the domain object where the name is “charlie.com”. ... `select` takes a boolean expression and, if that expression is true, returns that particular item. Everything else is filtered out. The `test` function takes a regex (or simple string to match).
🌐
GitHub
github.com › jqlang › jq › issues › 164
Feature request: regex (or minimally, startswith/endswith) string tests · Issue #164 · jqlang/jq
July 11, 2013 - String contains() is nice, but full regular-expression matching would be great. Or perhaps as smaller step: startswith/endswith or positional string-matching tests?
Author   gojomo
🌐
Debian Manpages
manpages.debian.org › testing › jq › jq.1.en.html
jq(1) — jq — Debian testing — Debian Manpages
The jq regex filters are defined so that they can be used using one of these patterns: STRING | FILTER(REGEX) STRING | FILTER(REGEX; FLAGS) STRING | FILTER([REGEX]) STRING | FILTER([REGEX, FLAGS]) ... FILTER is one of test, match, or capture, as described below.
🌐
DevCentral
community.f5.com › devcentral › articles › technical articles
iControl REST + jq Cookbook - Part 2: Intermediate | DevCentral
January 28, 2021 - Regular expressions? Yes, jq comes with standard regular expression functions such as test and match.
🌐
GitHub
github.com › jqlang › jq › issues › 2562
Regular expression ^ never matches after newline · Issue #2562 · jqlang/jq
March 30, 2023 - Describe the bug jq regular expressions are always handled as if "single line" mode is enabled. The "single line" flag has no effect. To Reproduce jq -n '"\nx"|[match("^x")]' Expected output: [ { "offset": 1, "length": 1, "string": "x", ...
Author   annettejanewilson