How do you transform JSON into CSV/TSV or group and aggregate with `jq`?
AdvancedConceptfind, xargs, jq and yq
Answer
# TSV of pods: namespace, name, node
kubectl get pods -A -o json |
jq -r '.items[] | [.metadata.namespace, .metadata.name, .spec.nodeName // "-"] | @tsv'
# Count pods per node
kubectl get pods -A -o json |
jq -r '.items | group_by(.spec.nodeName) | map({node: .[0].spec.nodeName, pods: length}) | .[] | "\(.node)\t\(.pods)"'
# Sum EBS volume sizes
aws ec2 describe-volumes | jq '[.Volumes[].Size] | add'
@csv quotes strings properly; @tsv escapes tabs; @sh quotes values for safe use in shell commands.
More on find, xargs, jq and yq
What are the most useful `find` predicates for operations work?BeginnerWhat is the difference between `-exec cmd {} \;`, `-exec cmd {} +` and `-delete`?IntermediateHow does `find` evaluate `-o` (OR) and why do you need parentheses?AdvancedWhat does `xargs` do, and which flags matter?IntermediateWhy is `find | xargs rm` dangerous and what is the safe form?IntermediateWhat is `jq` and what are the filters you use every day?Beginner