Leveraging jq for JSON Data Manipulation in Various AWS Services
Table of Contents
jq
GraphQL
{ viewer { commitComments(first: 10) { edges { node { author { login } commit { comments(first: 1) { edges { node { commit { message } } } } } } } } } }
{ "data": { "viewer": { "commitComments": { "edges": [ { "node": { "author": { "login": "jwalsh" }, "commit": { "comments": { "edges": [ { "node": { "commit": { "message": "Data files for Alexa" } } } ] } } } }, { "node": { "author": { "login": "jwalsh" }, "commit": { "comments": { "edges": [ { "node": { "commit": { "message": "Specify a minimum version for Buffer.allocUnsafe\n\nhttps://nodejs.org/api/buffer.html#buffer_class_method_buffer_allocunsafe_size" } } } ] } } } } ] } } } }
Extracting
cat response.json | jq '.data.foo.edges[]|.node'
Conversions
cat response.json | jq '.data.foo.edges[]|.node.bar|{id:.id,number:.number|tonumber}'
Filtering
cat response.json | jq '.data.foo.edges[]|.node.bar|{id:.id,number:.number|tonumber}|select(.number > 200)'
Recombining
cat response.json | jq '.data.foo.edges[]|.node.bar' | jq -s .
package.json
Contains
cat package-lock.json | jq '.dependencies[]|select(.resolved|contains("core-js"))'
AWS
RDS
Proof of Testing
aws rds describe-db-instances | jq -r '.DBInstances[]|[.DBInstanceIdentifier,.EngineVersion,.DBInstanceClass,.CACertificateIdentifier]|@csv'
ECR
Null and Select
aws ecr list-images --repository-name foo | jq '.imageIds[]|select(.imageTag != null)|select(.imageTag|contains("1.0.0"))'
SES
Sort and Raw Encoding
aws ses list-identities | jq -r '.Identities|sort|.[]'
Lambda
aws lambda list-functions | jq '[.Functions[].FunctionName]|sort'
CSV
aws lambda list-functions | jq -r '.Functions[]|[.FunctionName,.LastModified]|@csv'
Secrets Manager
aws secretsmanager list-secrets | jq -r '.SecretList[]|select(.Name=="dev" or .Name=="qa" or .Name=="stg")|.ARN'| cut -d : -f 7 | cut -d '-' -f 1
#!/bin/sh for E in $(aws secretsmanager list-secrets | jq -r '.SecretList[]|.Name'); do echo $E aws secretsmanager get-secret-value --secret-id $E | jq '.SecretString|fromjson' done