Skip to content

Instantly share code, notes, and snippets.

@marjamis
Last active December 21, 2023 04:55
Show Gist options
  • Save marjamis/603706548c4237dd3dde11c0fbdfc2b3 to your computer and use it in GitHub Desktop.
Save marjamis/603706548c4237dd3dde11c0fbdfc2b3 to your computer and use it in GitHub Desktop.
JMESPath and JQ Examples
aws ec2 describe-instances --query 'Reservations[].Instances[].[PrivateIpAddress, MetadataOptions]' --instance-ids <ids>
# Find all EIPs in a region which doesn't have an Association Id
aws ec2 describe-addresses --public-ips | jq '[.Addresses[] | select(.AssociationId == null)'
# Loops through subobjects of a given parent object when the names are dynamic. Basically getting a list of keys and then going through each key to get the required data.
jq '[.[].namedSubObjects | keys[] as $k | .[$k].data]' sample.queries
# Creates a new key for an object
jq '.[] | {newNameofSubObjects: .namedSubObjects}' sample.queries
# Loops through some objects and for .data provides a new key
jq '[.[].namedSubObjects | keys[] as $k | .[$k] | {newName: .data}]' sample.queries
# Get some Batch Job information, strips of the 3 last digits (for an epoch time), converts and calculates the differences between the end and start of the run in minutes
jq '[.[].Jobs[] | {JobId: .JobId, StartedAt: .StartedAt[:-3], StoppedAt: .StoppedAt[:-3], JobRunTimeInMins: (((.StoppedAt[:-3] | tonumber) - (.StartedAt[:-3] | tonumber)) / 60)}]' sample.queries
# Scan a DDB table to get data, such as: aws dynamodb scan --table-name <table> --query "Items[*]" > data.json and convert to csv in a specific order
jq -r 'map([
.service.S,
.Total.N,
.Active.N,
.preciseDate.S,
.date.S] | join(",")) | join("\n")' data.json > data.csv
# Calls inbuilt date function and creates a new list of objects from a subset object attributes of details in the source data
jq '[{
date: now | strftime("%Y-%m-%d %H:%M:%S"),
Jobs: [ .[].Jobs[] | {
JobId: .JobId,
StartedTime: .StartedAt
}]
}]' sample.queries
[
{
"namedSubObjects": {
"1": {
"data": "abcd"
},
"b": {
"data": "efgh"
},
"yea": {
"data": "ijkl"
}
},
"Jobs": [
{
"JobId": "jobId1",
"StartedAt": "1587635574131",
"StoppedAt": "1587678738881"
},
{
"JobId": "jobId3",
"StartedAt": "1587635574131",
"StoppedAt": "1587678738881"
},
{
"JobId": "jobId2",
"StartedAt": "1587635574131",
"StoppedAt": "1587678738881"
}
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment