None of your data contains "10.32.0.4:8055 10.32.0.7:8055".

You could pass multiple strings to contains(), using a bash array:

strings=("10.32.0.4:8055" "10.32.0.7:8055")

echo "${_metadatadashjson}" |
jq -r --args '.dashboard.panels[] | select(.targets[].expr | contains($ARGS.positional[])) | .targets[].expr' "${strings[@]}"

But contains will evaluate to true for each match. Ie. if one expr contained both strings, it would be selected (and printed) twice.

With test, that won't happen. Here's how you can add the |s between multiple strings, and pass them in a single jq variable (as well as escape all the dots):

strings=("10.32.0.4:8055" "10.32.0.7:8055")
IFS=\|
echo "${_metadatadashjson}" |
jq -r --arg str "${strings[*]//./\\.}" '.dashboard.panels[] | select(.targets[].expr | test($str)) | .targets[].expr'

Both examples print this:

jvm_threads_current{instance="10.32.0.4:8055",job="prometheus_gyrex"}
jvm_threads_current{instance="10.32.0.7:8055",job="prometheus_gyrex"}

Update: I forgot to escape the dots for test. I edited the test example so that all the dots get escaped (with a single backslash). It's regex, so (unescaped) dots will match any character. The contains example matches the strings literally (not regex).

Answer from dan on Stack Overflow
🌐
jq
jqlang.org › manual
jq 1.8 Manual
To capture all the matches for each input string, use the idiom [ expr ], e.g. [ scan(regex) ]. If the regex contains capturing groups, the filter emits a stream of arrays, each of which contains the captured strings. ... Splits an input string on each regex match. For backwards compatibility, when called with a single argument, split splits on a string, not a regex. ... These provide the same results as their split counterparts, but as a stream instead of an array. ... Emit the string obtained by replacing the first match of regex in the input string with tostring, after interpolation. tostring should be a jq string or a stream of such strings, each of which may contain references to named captures.
Top page
jq is a lightweight and flexible command-line JSON processor
Download
Docker image is available from GitHub Container Registry. docker run -i --rm ghcr.io/jqlang/jq -n 'range(3)'
Tutorial
jq is a lightweight and flexible command-line JSON processor
🌐
Bass Pro Shops
basspro.com › shop › en › 102001462
Remington~870~12 Gauge
Shop Bass Pro Shops for fishing, hunting, camping and boating gear, apparel and outdoor lifestyle essentials trusted by outdoor enthusiasts.
🌐
LobeHub
lobehub.com › home › skills › session-logs
Lobehub
3 weeks ago - Requires jq and a standard JSON/JSONL session log format. SKILL.md · Search your complete conversation history stored in session JSONL files. Use this when you need to recall something not in your memory files. Session logs live at: ~/.surprisebot/agents/main/sessions/ sessions.json - Index mapping session keys to session IDs · <session-id>.jsonl - Full conversation transcript per session · Each .jsonl file contains messages with: type: "session" (metadata) or "message" timestamp: ISO timestamp ·
Top answer
1 of 2
2

None of your data contains "10.32.0.4:8055 10.32.0.7:8055".

You could pass multiple strings to contains(), using a bash array:

strings=("10.32.0.4:8055" "10.32.0.7:8055")

echo "${_metadatadashjson}" |
jq -r --args '.dashboard.panels[] | select(.targets[].expr | contains($ARGS.positional[])) | .targets[].expr' "${strings[@]}"

But contains will evaluate to true for each match. Ie. if one expr contained both strings, it would be selected (and printed) twice.

With test, that won't happen. Here's how you can add the |s between multiple strings, and pass them in a single jq variable (as well as escape all the dots):

strings=("10.32.0.4:8055" "10.32.0.7:8055")
IFS=\|
echo "${_metadatadashjson}" |
jq -r --arg str "${strings[*]//./\\.}" '.dashboard.panels[] | select(.targets[].expr | test($str)) | .targets[].expr'

Both examples print this:

jvm_threads_current{instance="10.32.0.4:8055",job="prometheus_gyrex"}
jvm_threads_current{instance="10.32.0.7:8055",job="prometheus_gyrex"}

Update: I forgot to escape the dots for test. I edited the test example so that all the dots get escaped (with a single backslash). It's regex, so (unescaped) dots will match any character. The contains example matches the strings literally (not regex).

2 of 2
0

The problem is that the string with the space in it does not in fact occur in the given JSON. It's not too clear what you are trying to do but please note that contains is not symmetric:

"a" | contains("a b")

evaluates to false.

If you intended to write a boolean search criterion, you could use a boolean expression, or use jq's regular expression machinery, e.g.

test("10.32.0.4:8055|10.32.0.7:8055")

or probably even better:

test("\"(10[.]32[.]0[.]4:8055|10[.]32[.]0[.]7:8055)\"")
🌐
GitHub
github.com › jqlang › jq › issues › 861
Print array element if it contains · Issue #861 · jqlang/jq
July 21, 2015 - I would like to be able to print out elements that have a value inside key that contains ".dar.". I think it would be possible if I was working with numbers in the values but cannot figure out how to do a regex, and using contains just prints true or false. I would basically like to know how to get the following output · { "key1": "lorem ipsum", "key2": "bler dar de" } { "key1": "blurb blurb blurb", "key2": "dar dar dar" } I would picture the command looking similar to this but cannot get anything to work · cat JSONFile.json | jq '.arrayOfStuff | if .[].key2 =~ /.*dar.*/ then .[] end' Anybody know of a way to do this?
Author   miked63017
Find elsewhere
🌐
DevDocs
devdocs.io › jq
DevDocs — jq documentation
jq 1.7 API documentation with instant search, offline support, keyboard shortcuts, mobile version, and more.
🌐
jQuery
api.jquery.com › contains-selector
:contains() Selector | jQuery API Documentation
Categories: Selectors > Content Filter | Selectors > jQuery Extensions · Description: Select all elements that contain the specified text.
🌐
GitHub
github.com › jqlang › jq › issues › 1711
Confusing contains() behavior · Issue #1711 · jqlang/jq
August 20, 2018 - When contains() is applied against an array of strings, then the user could reasonably expect that the array values are checked for exact-match against the search string. In fact, when jq runs this kind of query, the contains() is applie...
Author   mcandre
🌐
Cabela's
cabelas.com › shop › en › 102001460
ROHM-GMBH Sonth~None~.22 Long Rifle
Shop Cabela’s for legendary hunting, fishing, camping and boating gear, apparel and outdoor essentials trusted by generations of outdoorsmen.
🌐
GitHub
github.com › jqlang › jq › issues › 33
filter on "string contains" · Issue #33 · jqlang/jq
October 23, 2012 - Perhaps I'm missing it, but I can't see a way to filter based on a value containing a string. Something like jq 'select(.msg.indexOf('Me") != -1)' Input [ {"msg": "Pick Me!", "date": "2012-10-22 04:42:05"}, {"msg": "Not this one","date":...
Author   billehunt
🌐
GitHub
github.com › jqlang › jq › issues › 2382
`contains(element)` incorrectly returns `true` when object value doesn't match · Issue #2382 · jqlang/jq
December 9, 2021 - Describe the bug I've noticed that contains(element), when given an object as an argument with a value that is of type string, will incorrectly return true when the JSON being parsed doesn't contain the value we're looking for. ... echo '{"foo": "bar", "baz": "qux", "beep": "stuff_in_front_of_boop"}' | jq 'contains({"foo":"bar", "beep":"boop"})'
Author   Integralist
🌐
LobeHub
lobehub.com › home › skills › log-dive
log-dive | Skills Marketplace · LobeHub
3 weeks ago - {app="myapp"} |= "error" # line contains "error" {app="myapp"} != "healthcheck" # line does NOT contain {app="myapp"} |~ "error|warn" # regex match on line {app="myapp"} !~ "DEBUG|TRACE" # negative regex
🌐
Bartificer
pbs.bartificer.net › pbs158
PBS 158: More Queries (jq)
December 20, 2023 - When the input being processed and the argument are strings, contains will return true if the input string contains the entire argument string (contiguously), otherwise it will return false: echo '"I love waffles"' | jq 'contains("waffles")' ...
🌐
Atlassian Support
support.atlassian.com › atlassian support › jira service management › resources › search for issues in jira
Use advanced search with Jira Query Language (JQL) | Jira Service Management Cloud | Atlassian Support
The advanced search is the most powerful and flexible way to search for your work items in Jira. You can use the Jira Query Language (JQL) to specify criteria that cannot be defined in the quick or basic searches.
🌐
Cabela's
cabelas.com › shop › en › 101977315
Ruger ~ Max-9 ~ 9mm Luger
Shop Cabela’s for legendary hunting, fishing, camping and boating gear, apparel and outdoor essentials trusted by generations of outdoorsmen.