Jq Notes: Difference between revisions
From Federal Burro of Information
Jump to navigationJump to search
No edit summary |
No edit summary |
||
Line 32: | Line 32: | ||
aws ec2 describe-instances --query 'Reservations[].Instances[].[PrivateIpAddress,Tags[?Key==`Name`].Value | [0], State.Name]' --output json | \ | aws ec2 describe-instances --query 'Reservations[].Instances[].[PrivateIpAddress,Tags[?Key==`Name`].Value | [0], State.Name]' --output json | \ | ||
jq '.[] | join(",")' | jq '.[] | join(",")' | ||
== Getting the project id out of a gitlab runner detail view == | |||
curl -s --header "PRIVATE-TOKEN: ${TOKEN}" "https://gitlab.com/api/v4/runners/XXX" | |||
gives: | |||
<pre> | |||
... PREAMBLE | |||
"projects": [ | |||
{ | |||
"id": XXX, | |||
"description": "", | |||
"name": "MyProject", | |||
STUFF | |||
}, | |||
... MORE | |||
</pre> | |||
we want to remove the dead runenr, but it's associated with many project, so we want to extract just the project ids for iteration over. | |||
Like this: | |||
curl -s URL | jq '.projects[] | .id' | |||
XXX | |||
YYY | |||
== Also See == | == Also See == | ||
* [[AWS Notes]] | * [[AWS Notes]] |
Latest revision as of 16:12, 3 March 2020
Selecting name from tags:
aws ec2 describe-instances | jq '.Reservations[].Instances[] | select(.LaunchTime > "2015-01-28") | select(.State.Code != 48) | [(.Tags[]|select(.Key=="Name")|.Value),.LaunchTime, .State.Name] | join(", ")'
to CSV (join on the end ):
azure location list --json | jq '.[] | .name | join(", ")'
AWS account report:
cat get_accounts_v4.json | jq '.accounts_and_users | .[] | select(.aws_account_id != null) | { account_name: .account_name, aws_account_id: .aws_account_id }| join(",")' > aws_accounts.report
Azure account report:
cat get_accounts_v4.json | jq '.accounts_and_users | .[] | select(.azure_account_id != null) | { account_name: .account_name, azure_account_id: .azure_account_id }| join(",")' > azure_accounts.report
using jq to make aws json into csv
today's aws cli tip:
You are making a list of instances for a report.
aws ec2 describe-instances --query 'Reservations[].Instances[].[PrivateIpAddress,Tags[?Key==`Name`].Value | [0], State.Name]' --output text
Everything is great, except the instances "Name" tag has spaces in it.
Now you are importing it into Excel and it gets all mashed up. You want csv. But aws cli doesn't have an --output csv.
To the rescue jq ( Jquery ) (the awk of js).
aws ec2 describe-instances --query 'Reservations[].Instances[].[PrivateIpAddress,Tags[?Key==`Name`].Value | [0], State.Name]' --output json | \ jq '.[] | join(",")'
Getting the project id out of a gitlab runner detail view
curl -s --header "PRIVATE-TOKEN: ${TOKEN}" "https://gitlab.com/api/v4/runners/XXX"
gives:
... PREAMBLE "projects": [ { "id": XXX, "description": "", "name": "MyProject", STUFF }, ... MORE
we want to remove the dead runenr, but it's associated with many project, so we want to extract just the project ids for iteration over. Like this:
curl -s URL | jq '.projects[] | .id' XXX YYY