Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 527

Query/update json file where key has multiple dots in name

$
0
0

Given test file: sample.json:

{"host_config": {"host1.reg.fqdn.com": {"config1": "AAA","config2": "000"},"host2.reg.fqdn.com": {"config1": "BBB","config2": "000"},"host3.reg.fqdn.com": {"config1": "CCC","config2": "000"     }  }}

I need to update "config2" for each of the hostname keys but i cannot seem to get the correct jq syntax using the --arg var definition format for the hostnames. (using jq1.6)

jq '.host_config."host1.reg.fqdn.com".config2=1' sample.json                 good!jq --arg a "1" '.host_config."host1.reg.fqdn.com".config2=$a' sample.json    good!
jq --arg a "1" --arg b "host1.reg.fqdn.com" '.host_config."$b".config2=$a' sample.json  completes but does not sub in "host1.reg.fqdn.com" for b - it creates new key under .host_config like:  "$b": {"config2": "1"  }
jq --arg a "1" --arg b "host1.reg.fqdn.com" '.host_config.'$b'.config2=$a' sample.json  =\> jq errorjq --arg a "1" --arg b "host1.reg.fqdn.com" '.host_config.[$b].config2=$a' sample.json  =\> jq errorjq --arg a "1" --arg b "host1.reg.fqdn.com" '.host_config.['$b'].config2=$a' sample.json  =\> jq errorjq --arg a "1" --arg b "host1.reg.fqdn.com" '.host_config.["$b"].config2=$a' sample.json  =\> jq errorjq --arg a "1" --arg b "host1.reg.fqdn.com" '.host_config.'"$b"'.config2=$a' sample.json =\> jq errorjq --arg a "1" --arg b "host1.reg.fqdn.com" '.host_config."'$b'".config2=$a' sample.json =\> completes butcreates:"": {"config2": "1"  }

Ultimately what I want to do is something like:

for i in 1..3 ; do   jq --arg a "$i" --arg b "host${i}.reg.fqdn.com" '.host_config."$b".config2=$a' sample.jsondone

Viewing all articles
Browse latest Browse all 527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>