I am doing curl
request and getting response:
{
"data": [
{
"id": "run-12345",
"message": "some message",
"created-by": "me",
"attributes": {
"is-cancelable": false,
"is-confirmable": false,
"is-discardable": false,
"is-force-cancelable": false
},
"relationships": {
"configuration-version": {
"data": {
"id": "cv-23198",
"type": "configuration-version"
}
}
}
},
{
"id": "run-67891",
"message": "some message",
"created-by": "me",
"attributes": {
"is-cancelable": false,
"is-confirmable": false,
"is-discardable": false,
"is-force-cancelable": false
},
"relationships": {
"configuration-version": {
"data": {
"id": "cv-12198",
"type": "configuration-version"
}
}
}
}
],
"included": [
{
"id": "cv-23198",
"type": "ingress-attributes",
"attributes": {
"branch": "prod",
"clone-url": "ssh://git@github/project/repo-awesome.git",
"commit-message": "Commit message 1",
"commit-sha": "4444444444444444444444f601bb4bcd5e07f02a",
"sender-username": "Name Surname"
}
},
{
"id": "cv-12198",
"type": "ingress-attributes",
"attributes": {
"branch": "dev",
"clone-url": "ssh://git@github/project/less-awesome.git",
"commit-message": "Commit message 3",
"commit-sha": "eeeeeeeeeeeeeeeeeeee44f601bb4bcd5e07f02a",
"sender-username": "Joe Sur"
}
}
]
}
My goal is to use jq
to kind of iterate over key "data"
and select important info about particular "id"
and then some additional information related to this very "id"
is also stored in this other main key "included"
and it is id
‘ed via "ww-212121"
.
Is there any way to kind of get or map data from this two sources together ?
I tried something like this but it obviously does not work
curl <http://...>| jq -r '.data[] | {"run-id": .id , message: .message, "created-by": ."created-by", cv: .relationships."configuration-version".data.id}'
{
"run-id": "run-12345",
"message": "some message",
"created-by": "me",
"cv": "cv-23198",
"branch": "prod",
"sender-username": "Name Surname"
}
{
"run-id": "run-67891",
"message": "some message",
"created-by": "me",
"cv": "cv-12198",
"branch": "dev",
"sender-username": "Joe Sur"
}
I cannot find a way how to enrich this jq so I will have the info about a branch and sender-username taken from keyword "included"
2
Answers
Finally, after reading JQ docs many times I fond a solution to my question:
Simplified version thanks to rickhg12hs: