Command-line JSON processor
General
If downloading some JSON content from the internet you mostly get a long string in one line which is hard to read
# JSON=$(curl --silent https://config.zscaler.com/api/zscaler.net/hubs/cidr/json/recommended)
# echo $JSON
{"cloudName":"zscalerthree.net","type":"recommended","hubPrefixes":["8.25.203.0/24","64.74.126.64/26","70.39.159.0/24","72.52.96.0/26","87.58.64.0/18","89.167.131.0/24","101.2.192.0/18","104.129.192.0/20","112.196.99.180/32","136.226.0.0/16","137.83.128.0/18","147.161.128.0/17","165.225.0.0/17","165.225.192.0/18","167.103.0.0/16","167.106.0.0/16","170.85.0.0/16","185.46.212.0/22","194.9.96.0/20","194.9.112.0/22","194.9.116.0/24","198.14.64.0/18","199.168.148.0/22","205.220.0.0/17","209.55.128.0/18","209.55.192.0/19","213.152.228.0/24","216.52.207.64/26","216.218.133.192/26","2400:7aa0:131b::/48","2400:7aa0:131c::/48","2400:7aa0:1815::/48","2400:7aa0:1b11::/48","2400:7aa0:1b19::/48","2400:7aa0:1c11::/48","2400:7aa0:1c12::/48","2400:7aa0:1c16::/48","2400:7aa0:1c17::/48","2400:7aa0:1e11::/48","2400:7aa0:1e18::/48","2605:4300:1211::/48","2605:4300:1212::/48","2605:4300:1214::/48","2605:4300:1216::/48","2605:4300:1311::/48","2605:4300:1312::/48","2605:4300:1315::/48","2605:4300:131b::/48","2605:4300:131c::/48","2605:4300:1411::/48","2605:4300:1412::/48","2605:4300:1413::/48","2605:4300:1414::/48","2605:4300:1418::/48","2605:4300:1420::/48","2605:4300:1421::/48","2a03:eec0:1211::/48","2a03:eec0:1212::/48","2a03:eec0:1216::/48","2a03:eec0:1311::/48","2a03:eec0:1312::/48","2a03:eec0:1317::/48","2a03:eec0:1411::/48","2a03:eec0:1414::/48","2a03:eec0:1415::/48","2a03:eec0:1d16::/48","2a03:eec0:1e15::/48","2a03:eec0:2e15::/48","2a03:eec0:3217::/48","2a03:eec0:321a::/48","2a03:eec0:321b::/48","2a03:eec0:321c::/48","2a03:eec0:321e::/48","2a03:eec0:321f::/48","2a03:eec0:3220::/48","2a03:eec0:3221::/48"]}
Use jq to process the string and print out formatted
# echo $JSON | jq .
{
"cloudName": "zscaler.net",
"type": "recommended",
"hubPrefixes": [
"8.25.203.0/24",
"64.74.126.64/26",
"70.39.159.0/24",
"72.52.96.0/26",
"87.58.64.0/18",
"89.167.131.0/24",
"101.2.192.0/18",
"104.129.192.0/20",
"112.196.99.180/32",
"136.226.0.0/16",
"137.83.128.0/18",
"147.161.128.0/17",
"165.225.0.0/17",
"165.225.192.0/18",
"167.103.0.0/16",
"167.106.0.0/16",
"170.85.0.0/16",
"185.46.212.0/22",
"194.9.96.0/20",
"194.9.112.0/22",
"194.9.116.0/24",
"198.14.64.0/18",
"199.168.148.0/22",
"205.220.0.0/17",
"209.55.128.0/18",
"209.55.192.0/19",
"213.152.228.0/24",
"216.52.207.64/26",
"216.218.133.192/26",
"2400:7aa0:131b::/48",
"2400:7aa0:131c::/48",
"2400:7aa0:1815::/48",
"2400:7aa0:1b11::/48",
"2400:7aa0:1b19::/48",
"2400:7aa0:1c11::/48",
"2400:7aa0:1c12::/48",
"2400:7aa0:1c16::/48",
"2400:7aa0:1c17::/48",
"2400:7aa0:1e11::/48",
"2400:7aa0:1e18::/48",
"2605:4300:1211::/48",
"2605:4300:1212::/48",
"2605:4300:1214::/48",
"2605:4300:1216::/48",
"2605:4300:1311::/48",
"2605:4300:1312::/48",
"2605:4300:1315::/48",
"2605:4300:131b::/48",
"2605:4300:131c::/48",
"2605:4300:1411::/48",
"2605:4300:1412::/48",
"2605:4300:1413::/48",
"2605:4300:1414::/48",
"2605:4300:1418::/48",
"2605:4300:1420::/48",
"2605:4300:1421::/48",
"2a03:eec0:1211::/48",
"2a03:eec0:1212::/48",
"2a03:eec0:1216::/48",
"2a03:eec0:1311::/48",
"2a03:eec0:1312::/48",
"2a03:eec0:1317::/48",
"2a03:eec0:1411::/48",
"2a03:eec0:1414::/48",
"2a03:eec0:1415::/48",
"2a03:eec0:1d16::/48",
"2a03:eec0:1e15::/48",
"2a03:eec0:2e15::/48",
"2a03:eec0:3217::/48",
"2a03:eec0:321a::/48",
"2a03:eec0:321b::/48",
"2a03:eec0:321c::/48",
"2a03:eec0:321e::/48",
"2a03:eec0:321f::/48",
"2a03:eec0:3220::/48",
"2a03:eec0:3221::/48"
]
}
Addressing
Nodes
If you want to print a single value node you can use
# echo $JSON | jq .cloudName
"zscaler.net"
Use ‘-r’ for raw output. This deletes garbage characters like quotation marks or commas
# echo $JSON | jq -r .cloudName
zscaler.net
If the node contains an array the array is printed.
As you can see, ‘-r’ does NOT delete quotation marks or commas and the like if an array is printed
# echo $JSON | jq -r .hubPrefixes
[
"8.25.203.0/24",
"64.74.126.64/26",
"70.39.159.0/24",
"72.52.96.0/26",
"87.58.64.0/18",
"89.167.131.0/24",
"101.2.192.0/18",
"104.129.192.0/20",
"112.196.99.180/32",
"136.226.0.0/16",
"137.83.128.0/18",
"147.161.128.0/17",
"165.225.0.0/17",
"165.225.192.0/18",
"167.103.0.0/16",
"167.106.0.0/16",
"170.85.0.0/16",
"185.46.212.0/22",
"194.9.96.0/20",
"194.9.112.0/22",
"194.9.116.0/24",
"198.14.64.0/18",
"199.168.148.0/22",
"205.220.0.0/17",
"209.55.128.0/18",
"209.55.192.0/19",
"213.152.228.0/24",
"216.52.207.64/26",
"216.218.133.192/26",
"2400:7aa0:131b::/48",
"2400:7aa0:131c::/48",
"2400:7aa0:1815::/48",
"2400:7aa0:1b11::/48",
"2400:7aa0:1b19::/48",
"2400:7aa0:1c11::/48",
"2400:7aa0:1c12::/48",
"2400:7aa0:1c16::/48",
"2400:7aa0:1c17::/48",
"2400:7aa0:1e11::/48",
"2400:7aa0:1e18::/48",
"2605:4300:1211::/48",
"2605:4300:1212::/48",
"2605:4300:1214::/48",
"2605:4300:1216::/48",
"2605:4300:1311::/48",
"2605:4300:1312::/48",
"2605:4300:1315::/48",
"2605:4300:131b::/48",
"2605:4300:131c::/48",
"2605:4300:1411::/48",
"2605:4300:1412::/48",
"2605:4300:1413::/48",
"2605:4300:1414::/48",
"2605:4300:1418::/48",
"2605:4300:1420::/48",
"2605:4300:1421::/48",
"2a03:eec0:1211::/48",
"2a03:eec0:1212::/48",
"2a03:eec0:1216::/48",
"2a03:eec0:1311::/48",
"2a03:eec0:1312::/48",
"2a03:eec0:1317::/48",
"2a03:eec0:1411::/48",
"2a03:eec0:1414::/48",
"2a03:eec0:1415::/48",
"2a03:eec0:1d16::/48",
"2a03:eec0:1e15::/48",
"2a03:eec0:2e15::/48",
"2a03:eec0:3217::/48",
"2a03:eec0:321a::/48",
"2a03:eec0:321b::/48",
"2a03:eec0:321c::/48",
"2a03:eec0:321e::/48",
"2a03:eec0:321f::/48",
"2a03:eec0:3220::/48",
"2a03:eec0:3221::/48"
]
Array members
If your node is an array you can also address single array members
Array index starts at 0
And as you can see below, the ‘-r’ parameter works for non arrays and single array members
# echo $JSON | jq .hubPrefixes[0]
"8.25.203.0/24"
# echo $JSON | jq -r .hubPrefixes[0]
8.25.203.0/24
If an array indey does not exist jq returns ‘null’ which gives you the opportunity to loop through arrays until jq returns ‘null’
# echo $JSON | jq -r .hubPrefixes[99]
null
You can also address array index ranges
where the first index ist the first in output array and the second index is the first one not in output array
So, the below example showas index 2-7
# echo $JSON | jq .hubPrefixes[2:8]
[
"70.39.159.0/24",
"72.52.96.0/26",
"87.58.64.0/18",
"89.167.131.0/24",
"101.2.192.0/18",
"104.129.192.0/20"
]
You can do crazy stuff with this. See ‘man jq’ for details.