0

I have a JSON response file of JIRA issues. I need to extract the JIRA issue key from it and store it in a variable. Below is the sample response file in which the JIRA issue key is CIJ-4

{"expand":"schema,names","startAt":0,"maxResults":50,"total":2,"issues":[{"expand":"operations,editmeta,changelog,transitions,renderedFields","id":"10100","self":"http://localhost:7080/rest/api/2/issue/10100","key":"CIJ-4","fields":{"issuetype":{"self":"http://localhost:7080/rest/api/2/issuetype/1","id":"1","description":"A problem which impairs or prevents the functions of the product.","iconUrl":"http://localhost:7080/images/icons/issuetypes/bug.png","name":"Bug","subtask":false},"components":[],"timespent":null,"timeoriginalestimate":null,"description":"Creating an issue via REST API","project":{"self":"http://localhost:7080/rest/api/2/project/10000","id":"10000","key":"CIJ","name":"CIJIRA","avatarUrls":{"48x48":"http://localhost:7080/secure/projectavatar?avatarId=10011","24x24":"http://localhost:7080/secure/projectavatar?size=small&avatarId=10011","16x16":"http://localhost:7080/secure/projectavatar?size=xsmall&avatarId=10011","32x32":"http://localhost:7080/secure/projectavatar?size=medium&avatarId=10011"}},"fixVersions":

In my example the request is CIJ-4, but as new requests gets logged in the numbers will increase CIJ-10,CIJ-100,CIJ-1000. So my challenge is how do I extract the exact JIRA issue key?

After I extract the result and store it in a file lets say result.txt, i need to parse the reuslt.txt and everytime there is a CIJ-#, i need to pass CIJ-# as a variable to another script

Jeel
  • 2,227
  • 5
  • 23
  • 38

1 Answers1

1

Simple grep version could be like following:

grep -oE "\"key\":\"CIJ-[0-9]*\"" data.txt | awk -F':' '{print $2}'
Murad Tagirov
  • 776
  • 6
  • 10
  • 1
    [JQ][1] is great little command line tool for processing JSON documents. For the sample you provide, the following command will extract the issue keys for all issues in the results: jq -r '.issues[] | .key' < data.txt You can pipe JSON directly from the JIRA JSON API using [curl][2] to download and extract data in one step. [1]: https://stedolan.github.io/jq/ [2]: http://curl.haxx.se/ – Richard Neish Nov 29 '15 at 20:17