【问题标题】:Parse JSON Data using Shell Scripting使用 Shell 脚本解析 JSON 数据
【发布时间】:2016-12-06 07:20:15
【问题描述】:

如何使用 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 等等..

【问题讨论】:

  • 使用合适的json解析工具,比如jq

标签: bash shell sh


【解决方案1】:

假设像这样的有效 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

【讨论】:

    【解决方案2】:

    您可以使用grepcut 轻松做到这一点:

    [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
    

    【讨论】:

    • 我使用了上面的命令,但没有显示任何输出。我使用的命令 "grep -o 'key:[-A-Z0-9]*' /Users/local/Documents/jira | cut -d: -f2"
    • 您使用的是哪个版本的grepgrep -V另外,您确定您的数据与您的样本数据匹配吗?
    • 是的,我确信数据是一样的。 grep 版本 -- 'grep (GNU grep) 2.20'
    • 应该没问题。如果您将测试数据放入文件中,然后在其上运行命令,它会返回结果吗?另外,它必须是bash吗?由于它是 JSON,您可以轻松地使用 Perl 或 PHP 或其他东西加载它...
    • 对不起,我已经再次验证了数据。
    【解决方案3】:

    可以将nodejsHere Documents 一起使用,例如:

    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) $ 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 2014-05-05
      • 1970-01-01
      • 2011-02-08
      相关资源
      最近更新 更多