The following code works, but what is the industry-standard way of doing this? I'm more comfortable with languages that return FALSE when they fail. The idea of comparing a string with "null" doesn't sit right with me. Is jq actually returning the string "null" or is bash translating the false/empty return to that string?
Note, the array is nested in the JSON file and has an unknown number of elements.
#!/bin/bash
NEW_PEERS_JSON="${CNODE_HOME}/files/peers/20201309.json"
index=0
while [ 1 ]
do
addr=$(jq -r .Producers[$index].addr < $NEW_PEERS_JSON)
if [ "$addr" == "null" ]
then
break
fi
echo $addr
index=$index+1
done
the JSON file looks like this:
{ "resultcode": "201", "networkMagic": "764824073", "ipType":4, "Producers": [
{ "addr": "xx.xxx.xxx.xx", "port": 3030, "valency": 1, "distance":14 },
{ "addr": "xx.xxx.xxx.xx", "port": 3001, "valency": 1, "distance":955 },
{ "addr": "xx.xxx.xxx.xx", "port": 3001, "valency": 1, "distance":1307 },
{ "addr": "xx.xxx.xxx.xx", "port": 3001, "valency": 1, "distance":1976 },
{ "addr": "xx.xxx.xxx.xx", "port": 4251, "valency": 1, "distance":3025 },
{ "addr": "xx.xxx.xxx.xx", "port": 3001, "valency": 1, "distance":3497 },
{ "addr": "xx.xxx.xxx.xx", "port": 3001, "valency": 1, "distance":3539 },
{ "addr": "xx.xxx.xxx.xx", "port": 3301, "valency": 1, "distance":3685 },
{ "addr": "xx.xxx.xxx.xx", "port": 4002, "valency": 1, "distance":7303 },
{ "addr": "xx.xxx.xxx.xx", "port": 4601, "valency": 1, "distance":7737 },
{ "addr": "xx.xxx.xxx.xx", "port": 4001, "valency": 1, "distance":7866 }
] }
Thanks!