6
{"expand":"renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations","id":"15114","self":"https://brg-jira-tst.state.mi.us/rest/api/2/issue/15114","key":"BRGTEST-11","fields":{"issuetype":{"self":"https://brg-jira-tst.state.mi.us/rest/api/2/issuetype/10200","id":"10200","description":"A task that needs to be done associated with Bridges project","iconUrl":"https://brg-jira-tst.state.mi.us/secure/viewavatar?size=xsmall&avatarId=10318&avatarType=issuetype","name":"Task","subtask":false,"avatarId":10318},"customfield_11500":"QAT"}}

Above is my json response which is stored in a.json

I want to extract the value of customfield_11500 from this a.json response using shell script. How to do it

In this case the Output for my shell command must give the result as "QAT"


Formatted JSON for the scrolling-averse:

{
  "expand": "renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations",
  "id": "15114",
  "self": "https://brg-jira-tst.state.mi.us/rest/api/2/issue/15114",
  "key": "BRGTEST-11",
  "fields": {
    "issuetype": {
      "self": "https://brg-jira-tst.state.mi.us/rest/api/2/issuetype/10200",
      "id": "10200",
      "description": "A task that needs to be done associated with Bridges project",
      "iconUrl": "https://brg-jira-tst.state.mi.us/secure/viewavatar?size=xsmall&avatarId=10318&avatarType=issuetype",
      "name": "Task",
      "subtask": false,
      "avatarId": 10318
    },
    "customfield_11500": "QAT"
  }
}
1
  • 1
    JSON is complex enough to require the use of a JSON-specific parser. I would use jq or, if you can't install that, python or perl. Jan 10, 2019 at 23:52

3 Answers 3

7

With recent versions of the ksh93 shell (v- or above):

read -m json j < file.json &&
  print -r -- "${j.fields.customfield_11500}"

Or use the widely available (though generally not installed by default) jq json processor tool:

jq -r '.fields.customfield_11500' file.json
7

Based in this post and using the formatted json file

grep -oP '(?<="customfield_11500": ")[^"]*' a.json
2
  • I don’t want to to use customfield_11500 directly.I will pass it to variable a and then I want to grep the value using $a.Kindlt let me know how to grep the value from variable
    – Chai
    Jan 12, 2019 at 20:54
  • @Chai Well you could put this in a bash script and create a method called key or something that does this, and replace '(?<="customfield_11500": ")[^"]*' with '(?<="'$1'": ")[^"]*' and then call key your_key I think, as long as you source your bash script, this might not work exactly
    – Sam
    Sep 12, 2021 at 2:42
0

here's an easy alternative solution based on jtc:

bash $ cat a.json | jtc -w'<customfield_11500>l'
"QAT"
bash $ 

JSON structures must be processed only by JSON aware routines (otherwise false-positives will be inevitable)

2
  • Why the cat there?
    – Kusalananda
    Jan 11, 2019 at 18:10
  • not required (just my way of building examples), one could do it like: jtc -w'<customfield_11500>l' a.json
    – Dmitry L.
    Jan 11, 2019 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.