Messing with JQ
Posted on Sun 21 October 2018 in Computing
JSON seems to be the goto text format these days. I wasn't a massive fan because I think YAML is nicer to read and XML I still think is great (think XPATH, XSTL etc) albeit not very hipster
Anyway, tools are what tends to make a technology IMHO and JQ is excellent once you do a bit of reading. This tutorial I particularly liked - https://programminghistorian.org/en/lessons/json-and-jq
Here's a couple of examples that are useful. I was playing with the Digital Ocean API
e.g.
curl -s -X GET -H "Content-Type: application/json" -H \
"Authorization: Bearer ${DO_API_TOKEN}" \
"https://api.digitalocean.com/v2/images?type=distribution" \
| jq '.images[] | {distribution: .distribution, id: .id, name: .name}'
This was a quick way for me to transform the distributions output into something human readable
curl -s -X GET -H "Content-Type: application/json" -H \
"Authorization: Bearer ${DO_API_TOKEN}" \
"https://api.digitalocean.com/v2/images?type=distribution" \
| jq '.images[] | select(.distribution == "CentOS") | {distribution: .distribution, id: .id, name: .name}'
This one just gets the slug names which I can set as a Terraform datasource
curl -s -X GET -H "Content-Type: application/json" -H \
"Authorization: Bearer ${DO_API_TOKEN}" \
"https://api.digitalocean.com/v2/images?type=distribution" \
| jq '.images[] | .slug'