I would like to run a bash command for each field in a JSON formatted piece of data by leveraging jq.
{
"apps": {
"firefox": "1.0.0",
"ie": "1.0.1",
"chrome": "2.0.0"
}
}
Basically I want something of the sort:
foreach app:
echo "$key $val"
done
Here is an bash script which demonstrates a possible solution.
#!/bin/bash
json='
{
"apps": {
"firefox": "1.0.0",
"ie": "1.0.1",
"chrome": "2.0.0"
}
}'
jq -M -r '
.apps | keys[] as $k | $k, .[$k]
' <<< "$json" | \
while read -r key; read -r val; do
echo "$key $val"
done
Example output
chrome 2.0.0
firefox 1.0.0
ie 1.0.1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With