gron
is a brew
installable tool that works on json data. Particularly useful to extract key:value from large json documents. Essentially gron
flattens the json. This is surprisingly useful when creating shell tools that have to deal with json
.
pipe that json into gron
garymbp:~ gary$ curl -S -u "$username:$password" -k -X GET --header 'Accept: application/json' $URL 2>/dev/null | gron | tail
json.metadata = {};
json.metadata.count = 1;
json.metadata.end_index = 1;
json.metadata.filter_criteria = "";
json.metadata.grand_total_entities = 5;
json.metadata.page = 1;
json.metadata.search_string = "ctr1";
json.metadata.sort_criteria = "";
json.metadata.start_index = 1;
json.metadata.total_entities = 1;
For instance let’s say we want to see all the json fields that contain the word iops
regardless of where it appears in the structure. We can simply pipe the json
into gron
then into gre
p like we have always done.
garymbp:~ gary$ curl -S -u "$username:$password" -k -X GET --header 'Accept: application/json' $URL 2>/dev/null | gron | grep iops
json.entities[0].stats.controller_num_iops = "65066";
json.entities[0].stats.controller_num_read_iops = "39096";
json.entities[0].stats.controller_num_write_iops = "25969";
json.entities[0].stats.hypervisor_num_iops = "-1";
json.entities[0].stats.hypervisor_num_read_iops = "-1";
json.entities[0].stats.hypervisor_num_write_iops = "-1";
json.entities[0].stats.num_iops = "-1";
json.entities[0].stats.num_read_iops = "-1";
json.entities[0].stats.num_write_iops = "-1";
Using the output from gron
we can now easily figure out what we need to feed into jq
to get the field we want without having to parse the json
structure too much. Most of the time just remove the word json
from the front then put the res in single quotes and pass to jq
. So in the case we want controller_num_iops
we know that we need to feed jq the full query .entities[0].stats.controller_num_iops
to jq
and we get our answer ""65066""
garymbp:~ gary$ curl -S -u "$username:$password" -k -X GET --header 'Accept: application/json' $URL 2>/dev/null | jq '.entities[0].stats.controller_num_iops'
"65066"
Proudly powered by WordPress