jq is a swiss army knife for working with JSON. It is especially handy for piping output of CLI tools, such as curl
ing JSON APIs, or aws
and az
CLIs.
I wanted to get a nice list of public IP addresses of my EC2 instances, together with instance names. I could have used boto
for this, but the combo of AWS CLI and jq
turned to be a simple and effective one-liner (split for better wrapping).
aws ec2 describe-instances | jq '.Reservations[].Instances[] |
{(.Tags[] | select (.Key == "Name") | .Value): .PublicIpAddress}' |
jq -s add
produces:
{
"foo": "54.131.121.177",
"bar": "52.75.8.58",
"baz": "34.228.156.28"
}