Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to join array elements with jq

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%3D60100&fields=key,status,fixVersions" -u user1 | jq -r '.issues[] | .key + "," + .fields.status.name + "," + .fields.fixVersions[].name'

Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

You can use join() such as

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

Try it online!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading