How to use jq as a professional
Advanced jq example that parses a Terraform Cloud API response to extract run details including branch, commit info, and status timestamps.
This advanced jq expression parses the Terraform Cloud API response (JSON:API format) to extract run metadata. It cross-references the included objects by ID to resolve nested relationships between runs, configuration versions, and ingress attributes, pulling out branch names, commit details, and status timestamps into a flat structure.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
curl -s \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/vnd.api+json" \
--request GET ${URL} | jq -r '(.included |
map({(.id): .}) | add) as $inc |
.data[] | {"run-id": .id} +
($inc[$inc[.relationships."configuration-version".data.id].relationships."ingress-attributes".data.id].attributes |
{
branch,
"commit-message",
"sender-username",
"clone-url",
"commit-url",
"compare-url",
identifier
}) +
(.attributes."status-timestamps"|
{
"applied-at",
"planned-at",
"queuing-at",
"applying-at",
"planning-at",
"confirmed-at",
"plan-queued-at",
"apply-queued-at",
"queuing-apply-at",
"plan-queueable-at",
}
) + (.attributes |
{
"trigger-reason"
}
)
'
This post is licensed under CC BY 4.0 by the author.