Been struggling with this for a while and I'm no closer to a solution. I'm not very experienced using jq
.
I'd like to take the values from one json file and add them to another file when other values in the dict match. The example files below demonstrate what I'd like more clearly than an explanation.
hosts.json:
{"hosts": [{"host": "hosta.example.com","hostid": "101","proxy_hostid": "1"},{"host": "hostb.example.com","hostid": "102","proxy_hostid": "1"},{"host": "hostc.example.com","hostid": "103","proxy_hostid": "2"}]}
proxies.json:
{"proxies": [{"host": "proxy1.example.com","proxyid": "1"},{"host": "proxy2.example.com","proxyid": "2"}]}
I also have the above file available with proxyid as the key, if this makes it easier:
{"proxies": {"1": {"host": "proxy1.example.com","proxyid": "1"},"2": {"host": "proxy2.example.com","proxyid": "2"}}}
Using these json files above (from the Zabbix API), I'd like to add the value of .proxies[].host
(from proxies.json
) as .hosts[].proxy_host
(to hosts.json
).
This would only be when .hosts[].proxy_hostid
equals .proxies[].proxyid
Desired output:
{"hosts": [{"host": "hosta.example.com","hostid": "101","proxy_hostid": "1","proxy_host": "proxy1.example.com"},{"host": "hostb.example.com","hostid": "102","proxy_hostid": "1","proxy_host": "proxy1.example.com"},{"host": "hostc.example.com","hostid": "103","proxy_hostid": "2","proxy_host": "proxy2.example.com"}]}
I've tried many different ways of doing this, and think I need to use jq -s
or jq --slurpfile
, but I've reached a lot of dead-ends and can't find a solution.
jq 'input as $p | map(.[].proxy_host = $p.proxies[].proxyid)' hosts.json proxies.json
I think I would need something like this as well, but not sure how to use it.
if .hosts[].proxy_hostid == .proxies[].proxyid then .hosts[].proxy_host = .proxies[].host else empty end'
I've found these questions but they haven't helped :(
- How do I use a value as a key reference in jq? <- I think this one is the closest
- Lookup values from one JSON file and replace in another
- Using jq find key/value pair based on another key/value pair
Best Answer
This indeed is easier with the alternative version of your proxies.json
. All you need is to store proxies in a variable as reference, and retrieve proxy hosts from it while updating hosts.
jq 'input as { $proxies } | .hosts[] |= . + { proxy_host: $proxies[.proxy_hostid].host }' hosts.json proxies.json
Online demo