skip to Main Content

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


  1. Chosen as BEST ANSWER

    Finally, after reading JQ docs many times I fond a solution to my question:

        cat example.json| jq -r '(.included | map({(.id): .}) | add)  as $inc | .data[] | {"run-id": .id , message: .message, "created-by": ."created-by", cv: .relationships."configuration-version".data.id, branch: $inc[.relationships."configuration-version".data.id].attributes.branch, "commit-message":$inc[.relationships."configuration-version".data.id].attributes."commit-message", "sender-username": $inc[.relationships."configuration-version".data.id].attributes."sender-username"  }'
    {
      "run-id": "run-12345",
      "message": "some message",
      "created-by": "me",
      "cv": "cv-23198",
      "branch": "prod",
      "commit-message": "Commit message 1",
      "sender-username": "Name Surname"
    }
    {
      "run-id": "run-67891",
      "message": "some message",
      "created-by": "me",
      "cv": "cv-12198",
      "branch": "dev",
      "commit-message": "Commit message 3",
      "sender-username": "Joe Sur"
    }
    

  2. Simplified version thanks to rickhg12hs:

    cat filtered.json | jq -r '(.included | 
      map({(.id): .}) | add) as $inc | 
      .data[] | {
        "run-id": .id, 
        "time": .attributes."status-timestamps"
      } + 
      ($inc[$inc[.relationships."configuration-version".data.id].relationships."ingress-attributes".data.id].attributes |
      {
        branch, 
        "commit-message", 
        "sender-username",
        "clone-url"
      })'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search