Update(start): answer the following line and you have answered the full question:
Why does the following:
echo '{"ipv4":["192.168.64.193"]}'|jq .ipv4[0]Give this error:
no matches found: .ipv4[0]Update(end)
I m trying to create a list variable, containing the values from "ipv4" from this json:
{
"list": [
{
"ipv4": [
"192.168.64.193"
],
"name": "node2",
"release": "20.04 LTS",
"state": "Running"
},
{
"ipv4": [
"192.168.64.192"
],
"name": "node1",
"release": "20.04 LTS",
"state": "Running"
}
]
}
HOSTS is a list of the ip addresses:
UPDATE: Thanks to @Kusalananda I am using this in the script HOSTS+=$(echo $i|jq '.ipv4[0]')
get_hosts(){
unset HOSTS
# unset NAMES
jq -c '.list[]' file.json | while read i; do
HOSTS+=$(echo $i | jq .ipv4)
# NAMES+=$(echo $i | jq .name)
done
echo $HOSTS
# echo $HOSTS
}
I get:
[
"192.168.64.193"
][
"192.168.64.192"
]
But I need:
"192.168.64.193"
"192.168.64.192"
So I tried:
...
HOSTS+=$(echo $i | jq .ipv4[0])
...
But then I get:
get_hosts:3: no matches found: .ipv4[0]
get_hosts:3: no matches found: .ipv4[0]
Note: this is one of the lines in the loop:
{"ipv4":["192.168.64.193"],"name":"node2","release":"20.04 LTS","state":"Running"}