Home > Blockchain >  How can I map/add field(s) from a node's parent into specific child leaves?
How can I map/add field(s) from a node's parent into specific child leaves?

Time:10-30

Given the following example JSON object (modeled after GitHub GraphQL query result):

Example

{
  "data": {
    "repository": {
      "releases": {
        "nodes": [
          {
            "tagName": "v2.0.0",
            "date": "2022-10-26T14:22:15Z",
            "releaseAssets": {
              "nodes": [
                {
                  "name": "foo-2.0.0.tar.gz",
                  "downloadUrl": "https://github.com/foo/foo-releases/releases/download/v2.0.0/foo-2.0.0.tar.gz"
                },
                {
                  "name": "foo-2.0.0-arm64.AppImage",
                  "downloadUrl": "https://github.com/foo/foo-releases/releases/download/v2.0.0/foo-2.0.0-arm64.AppImage"
                }
              ]
            }
          },
          {
            "tagName": "v1.3.0",
            "date": "2021-03-10T07:10:41Z",
            "releaseAssets": {
              "nodes": [
                {
                  "name": "foo-1.3.0.tar.gz",
                  "downloadUrl": "https://github.com/foo/foo-releases/releases/download/v1.3.0/foo-1.3.0.tar.gz"
                },
                {
                  "name": "foo-1.3.0-arm64.AppImage",
                  "downloadUrl": "https://github.com/foo/foo-releases/releases/download/v1.3.0/foo-1.3.0-arm64.AppImage"
                }
              ]
            }
          },
          {
            "tagName": "v0.9.9",
            "releaseAssets": {
              "nodes": []
            }
          }
        ]
      }
    }
  }
}

Can someone tell me what jq-fu will map the tagName and the date fields into each item in nodes[] and skip the empty nodes array to produce the following desired output?

Desired Output

[
  {
    "name": "foo-2.0.0.tar.gz",
    "downloadUrl": "https://github.com/foo/foo-releases/releases/download/v2.0.0/foo-2.0.0.tar.gz",
    "tagName": "v2.0.0",
    "date": "2022-10-26T14:22:15Z",
  },
  {
    "name": "foo-2.0.0-arm64.AppImage",
    "downloadUrl": "https://github.com/foo/foo-releases/releases/download/v2.0.0/foo-2.0.0-arm64.AppImage"
    "tagName": "v2.0.0",
    "date": "2022-10-26T14:22:15Z",
  },
  {
    "name": "foo-1.3.0.tar.gz",
    "downloadUrl": "https://github.com/foo/foo-releases/releases/download/v1.3.0/foo-1.3.0.tar.gz"
    "tagName": "v1.3.0",
    "date": "2021-03-10T07:10:41Z"
  },
  {
    "name": "foo-1.3.0-arm64.AppImage",
    "downloadUrl": "https://github.com/foo/foo-releases/releases/download/v1.3.0/foo-1.3.0-arm64.AppImage"
    "tagName": "v1.3.0",
    "date": "2021-03-10T07:10:41Z"
  }
]

How can I obtain the above output in the most efficient manner?

CodePudding user response:

You're looking for something like this:

.data.repository.releases.nodes | map({tagName, date}   .releaseAssets.nodes[])

Online demo

  • Related