0
votes

Using the Atlassian documentation, I'm able to create a new JIRA ticket using curl command. I want to understand how to read the response of this command into a variable? Essentially i want to store the JIRA ticket ID into a variable that can be used in other scripts.

Reference Link: https://developer.atlassian.com/server/jira/platform/jira-rest-api-examples/

_SAMPLE_CODE_

curl -D- -u charlie:charlie -X POST --data '{
    "fields": {
               "project":{
                  "key": "TEST"
               },
               "summary": "REST ye merry gentlemen.",
               "description": "Creating of an issue using project keys and issue type names using the REST API",
               "issuetype": {
                  "name": "Bug"
               }
            }
    }' -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue/

_SAMPLE_RESPONSE_

{
    "id":"39000",
    "key":"TEST-101",
    "self":"http://localhost:8080/rest/api/2/issue/39000"
}

From the above log, I want to understand how can we fetch the "key" (i.e. JIRA number or JIRA ID) from response (essentially stdout) into a variable.

1

1 Answers

0
votes

you can use jq command line tool to extract value from json:

#!/bin/bash


key=$(curl -s -u charlie:charlie -X POST --data '{
"fields": {
           "project":{
              "key": "TEST"
           },
           "summary": "REST ye merry gentlemen.",
           "description": "Creating of an issue using project keys and issue type names using the REST API",
           "issuetype": {
              "name": "Bug"
           }
        }
}' -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue/|jq -r .key)

echo "key: $key"

jq command is not installed by default on linux systems. you will need to install it manually:

sudo apt install jq
sudo yum install jq