How to extract a value from json response using shell script

36,646

Solution 1

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

Solution 2

Based in this post and using the formatted json file

grep -oP '(?<="customfield_11500": ")[^"]*' a.json
Share:
36,646

Related videos on Youtube

Chai
Author by

Chai

Updated on September 18, 2022

Comments

  • Chai
    Chai over 1 year
    {"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"
      }
    }
    
    • Angel Todorov
      Angel Todorov over 5 years
      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.
  • Kusalananda
    Kusalananda over 5 years
    Why the cat there?
  • Dmitry L.
    Dmitry L. over 5 years
    not required (just my way of building examples), one could do it like: jtc -w'<customfield_11500>l' a.json
  • Chai
    Chai over 5 years
    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
  • Sam
    Sam over 2 years
    @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