jq
Period in Key
{
"apiVersion": "v1",
"data": {
"tls.crt": "Zm9vCg==",
"tls.key": "YmFyCg=="
},
"kind": "Secret",
"type": "kubernetes.io/tls"
}
echo $KubeSecret | jq -r '.data["tls.crt"]'
Multiline
echo $GithubRelease \
| jq '.assets[]
| select(.name == "py-demo-Darwin-x86_64")
| .name'
Variable as Argument
echo $GithubRelease \
| jq --arg name "py-demo-Darwin" \
'.assets[]
| select(.name | startswith($name))
| .name'
Select
Name Contains
[
{"name": "This is item1"},
{"name": "This is item2"},
{"name": "item3"}
]
echo $Items \
| jq -c '.[] | select(.name | contains("item2"))'
Name Starts With
echo $Items \
| jq -c '.[] | select(.name | startswith("This is"))'
AWS Resources with Tag
Use aws --filter
Some AWS commands provide a --filter option that allows filtering by tag.
aws ec2 describe-addresses \
--filters "Name=tag:Name,Values=cfclrk"
Sorting
aws s3api list-objects-v2 \
--bucket $BUCKET \
--prefix "foo/bar" \
| jq '.Contents | sort_by(.LastModified)'
If you see an error like Cannot index string with string, it's probably
because you are not piping a proper list to it. For example:
cat $data | jq '.Stuff[] | sort_by(.Time)' # Does not work cat $data | jq '.Stuff | sort_by(.Time)' # Works!
Stringify JSON
{"message": "foo"}
echo $message | jq '@json'