Apologies if this is basic but the doc for jq is not so good i have this json:
{
"jsonrpc": "2.0",
"result": [{
"hostid": "10084",
"host": "Zabbix server",
"interfaces": [{
"interfaceid": "1",
"ip": "127.0.0.1"
}]
}, {
"hostid": "10336",
"host": "AUTO",
"interfaces": [{
"interfaceid": "4",
"ip": "1.2.3.4"
}]
}, {
"hostid": "10337",
"host": "AUTOSERVER",
"interfaces": [{
"interfaceid": "5",
"ip": "4.5.6.7"
}]
}, {
"hostid": "10348",
"host": "Server00001",
"interfaces": [{
"interfaceid": "16",
"ip": "4.5.6.7"
}]
}],
"id": 2
}
i need to find a way to use jq to find if "Server0001" exists in one of the hosts i know i can use grep but i prefer using jq here, like select.. any help or ref toa good doc would be much appriciated
any
(see the manual) can return a boolean value if a condition matches with at least one item.
jq 'any(.result[]; .host == "Server0001")'
false
jq 'any(.result[]; .host == "Server00001")'
true
You may also want to use the some parameters when invoking jq
(see the manual): The --arg
option, for instance, lets you add a variable which can be initialized from outside the filter string. And with the -e
(or --exit-status
) flag you can have jq
set the exit status according to the filter's final result. Together, this enables you to use jq
like this:
if jq --arg host "Server0001" -e 'any(.result[]; .host == $host)';
then
…
else
…
fi