シェルスクリプトを使用してjsonレスポンスから値を抽出する方法

シェルスクリプトを使用してjsonレスポンスから値を抽出する方法
{"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"}}

上記はa.jsonに保存されたjsonレスポンスです。

シェルスクリプトを使用して、a.jsonレスポンスからcustomfield_11500値を抽出したいと思います。それをする方法

この場合、シェルコマンドの出力は「QAT」結果を提供する必要があります。


スクロール嫌悪のためのJSON形式:

{
  "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

最新バージョンのksh93シェル(v-またはそれ以上):

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

または、広く利用可能な(通常はデフォルトではインストールされていない)jqjsonプロセッサツールを使用してください。

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

おすすめ記事