jq: error: xxx/0 is not defined at <top-level>, line 1:

jq命令处理特殊字符

不支持带有中横线的key-name,比如

"active-status"

json原文:

{
  "id": 1,
  "jsonrpc": "2.0",
  "result": {
    "active-status": "active",
    "booted-status": null,
    "current-epoch-performance": {
      "current-epoch-signing-percent": {
        "current-epoch-signed": 271040,
        "current-epoch-signing-percentage": "0.996866402836420343",
        "current-epoch-to-sign": 271892
      }
    },
    "currently-in-committee": true,
    "epos-status": "currently elected",
    "epos-winning-stake": "50093610622280299999999999.999999999999999992"
  }
}

正常情况:

抓取 id

ubuntu@harmony-validator-shard1-1:/data$ cat sign.json | jq .id
1

抓取 jsonrpc

ubuntu@harmony-validator-shard1-1:/data$ cat sign.json | jq .jsonrpc
"2.0"

抓取 result

ubuntu@harmony-validator-shard1-1:/data$ cat sign.json | jq .result
{
  "active-status": "active",
  "booted-status": null,
  "current-epoch-performance": {
    "current-epoch-signing-percent": {
      "current-epoch-signed": 271040,
      "current-epoch-signing-percentage": "0.996866402836420343",
      "current-epoch-to-sign": 271892
    }
  },
  "currently-in-committee": true,
  "epos-status": "currently elected",
  "epos-winning-stake": "50093610622280299999999999.999999999999999992"
}

特殊情况:抓取 active-status

ubuntu@harmony-validator-shard1-1:/data$ cat sign.json | jq .result.active-status
jq: error: status/0 is not defined at <top-level>, line 1:
.result.active-status               
jq: 1 compile error

纠正:需要使用中括号处理

ubuntu@harmony-validator-shard1-1:/data$ cat sign.json | jq '.result["active-status"]'
"active"

唯一正确写法:jq '.result["active-status"]'

下列写法全部错误!!!

jq '.result.["active-status"]'
jq '.result[active-status]'
jq .result["active-status"]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值