% jq -c '[path(.tags[]|select(contains(["tag:primary"])))[-1]]' hosts.json
["ams01","zrh02"]
2m28s from time I downloaded hosts.json to producing this solution, though I must admit I had to take a moment to look up contains to realized it had to take an array—I could've used index/1 and had a better time. % jq -c '[.hosts[]|select(.tags|index("tag:primary")).name]' hosts_advanced.json
["ams01","zrh02"]
This one was much quicker, just 1m5s, but of course after I'd already done the previous one.It's a language. You can learn it, and then it can be easier to write programs in it. Or you can blame the tool, sure.
edit: and for the curious, this is the path I took to produce these programs:
wget https://gist.github.com/ruuda/31cffffaa595492a335a4637503f4b7a/raw/7ae5bef0ac331eb2fb7d67cac6cc867e13a675f3/hosts.json
jq . hosts.json
jq .tags hosts.json
jq '.tags|path(.[]|select(contains("tag:primary")))' hosts.json
jq '.tags|path(.[]|select(contains(["tag:primary"])))' hosts.json
jq '.tags|path(.[]|select(contains(["tag:primary"])))[-1]' hosts.json
jq 'path(.tags[]|select(contains(["tag:primary"])))[-1]' hosts.json
jq '[path(.tags[]|select(contains(["tag:primary"])))[-1]]' hosts.json
jq -c '[path(.tags[]|select(contains(["tag:primary"])))[-1]]' hosts.json
jq -c '[path(.tags[]|select(index("tag:primary")|debug))[-1]]' hosts.json
jq -c '[path(.tags[]|select(index("tag:primary")))[-1]]' hosts.json
wget https://gist.github.com/ruuda/31cffffaa595492a335a4637503f4b7a/raw/7ae5bef0ac331eb2fb7d67cac6cc867e13a675f3/hosts_advanced.json
jq . hosts_advanced.json
jq '.hosts' hosts_advanced.json
jq '.hosts[]' hosts_advanced.json
jq '.hosts[]|select(.tags|index("tag:primary"))' hosts_advanced.json
jq '[.hosts[]|select(.tags|index("tag:primary")).name]' hosts_advanced.json
jq -c '[.hosts[]|select(.tags|index("tag:primary")).name]' hosts_advanced.json