如何使用shell脚本从下面的json数据解析字符串'key‘的值?
/Users/local/Documents/testjira:{"expand":"schema","names","startAt":"0","maxResults":"50","total":"2","issues":[{"expand":"operations","editmeta","changelog","transitions","renderedFields","id":"56392","self":"https://website.com/jira/rest/api/latest/issue/50342","key":"SAR-32"},{"expand":"operations","editmeta","changelog","transitions","renderedFields","id":"49799","self":"https://website.com/jira/rest/api/latest/issue/19720","key":"SAR-5"}]}示例输出: SAR-32、SAR-5等等。
发布于 2016-12-06 07:26:55
您可以使用grep和cut轻松地做到这一点:
[ttucker@localhost ~]$ cat /tmp/testjira2
{"expand":"schema","names","startAt":"0","maxResults":"50","total":"2","issues":[{"expand":"operations","editmeta","changelog","transitions","renderedFields","id":"56392","self":"https://website.com/jira/rest/api/latest/issue/50342","key":"SAR-32"},{"expand":"operations","editmeta","changelog","transitions","renderedFields","id":"49799","self":"https://website.com/jira/rest/api/latest/issue/19720","key":"SAR-5"}]}
[ttucker@localhost ~]$ grep -o '"key":"[^"]*"' /tmp/testjira2 |cut -d'"' -f4
SAR-32
SAR-5发布于 2016-12-06 12:27:38
假设有效的JSON类似
{
    "expand":["schema", "names"],
    "startAt":"0",
    "maxResults":"50",
    "total":"2",
    "issues":[
        {
            "expand":["operations","editmeta","changelog","transitions","renderedFields"],
            "id":"56392",
            "self":"https://website.com/jira/rest/api/latest/issue/50342",
            "key":"SAR-32"
        },
        {
            "expand":["operations","editmeta","changelog","transitions","renderedFields"],
            "id":"49799",
            "self":"https://website.com/jira/rest/api/latest/issue/19720",
            "key":"SAR-5"
        }
    ]
}可以使用以下对jq的调用
$ jq -r '.issues[] | .key' tmp.json
SAR-32
SAR-5发布于 2018-03-21 08:15:33
alec@mba ~/project/lnet (master) $ cat /Users/alec/project/lnet/test/rc/05\ Two\ hubs.json
{
  "desc": "This is a configuration file to run the test on all the hubs and leaves that can possibly participate",
  "hubs": { "hub0": "176.37.63.2", "hub1": "10.0.0.10" }
}
alec@mba ~/project/lnet (master) $ runAll() {
>   local conf="$HOME/project/lnet/$1"
>   echo "runAll: using configuration file $conf"
>   local output=$(node <<-EOF_JS
>     const conf = require("$conf")
>     console.log('hub0="' + conf.hubs.hub0 + '"')
> EOF_JS
> )
>   eval "$output"; echo "$hub0"
> }
alec@mba ~/project/lnet (master) $ runAll test/rc/05\ Two\ hubs.json
runAll: using configuration file /Users/alec/project/lnet/test/rc/05 Two hubs.json
176.37.63.2
alec@mba ~/project/lnet (master) $ https://stackoverflow.com/questions/40989881
复制相似问题