Home > other >  How to join array elements with jq
How to join array elements with jq

Time:02-03

I'm attempting to generate a .csv from json output in a specific format. The JSON is as follows:

{
  "expand": "names,schema",
  "startAt": 0,
  "maxResults": 50,
  "total": 1,
  "issues": [
    {
      "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
      "id": "42202",
      "self": "https://www.myjira.com/jira/rest/api/2/issue/42202",
      "key": "JIRAISSUE-369",
      "fields": {
        "fixVersions": [
          {
            "self": "https://www.myjira.com/jira/rest/api/2/version/15701",
            "id": "15701",
            "name": "MR1",
            "archived": false,
            "released": false,
            "releaseDate": "2014-06-10"
          },
          {
            "self": "https://www.myjira.com/jira/rest/api/2/version/15702",
            "id": "15702",
            "name": "MR2",
            "archived": false,
            "released": false,
            "releaseDate": "2014-04-14"
          }
        ],
        "status": {
          "self": "https://www.myjira.com/jira/rest/api/2/status/3",
          "description": "This issue is being actively worked on at the moment by the assignee.",
          "iconUrl": "https://www.myjira.com/jira/images/icons/statuses/inprogress.png",
          "name": "In Progress",
          "id": "3",
          "statusCategory": {
            "self": "https://www.myjira.com/jira/rest/api/2/statuscategory/4",
            "id": 4,
            "key": "indeterminate",
            "colorName": "yellow",
            "name": "In Progress"
          }
        }
      }
    }
  ]
}

I'd like to have a comma separated list with fixVersions' elements being comma separated. I've gotten this far.

curl -X GET -H "Content-Type: application/json" -k "https://www.myjira.com/jira/rest/api/2/search?jql=filter=60100&fields=key,status,fixVersions" -u user1 | jq -r '.issues[] | .key   ","   .fields.status.name   ","   .fields.fixVersions[].name'

Output:

JIRAISSUE-369,In Progress,MR1
JIRAISSUE-369,In Progress,MR2

Desired output:

JIRAISSUE-369,In Progress,MR1,MR2

How do I take each .issues.fixVersions[].name and comma separate them at the end?

CodePudding user response:

You can use join() such as

jq '.issues[] | [.key , .fields.status.name , .fields.fixVersions[].name] | join(",")'

Try it online!

  •  Tags:  
  • Related