小编典典

jq无法使用带破折号和数字的标记名称

json

我正在使用jq,但json标记中包含“-”使jq无法编译。我无法逃脱它使其工作。这里的命令

    curl -X GET -H "X-AppKey:foo" "foo/v2/_status" | jq '.component-status[]'

我已经在jq的github上阅读了这篇帖子https://github.com/stedolan/jq/issues/202,但我无法使其正常运行。

这是curl的输出

   {
  "status": "ok",
  "hostname": "0b0b495a46db",
  "component-status": [
   {
     "status-code": 200,
     "component": "Service1",
     "status": "OK"
   },
   {
     "status-code": 200,
     "component": "Service2",
     "status": "OK"
   }
  ]
 }

任何想法?


阅读 254

收藏
2020-07-27

共1个答案

小编典典

您需要用方括号和双引号引起来:

jq '."component-status"'

使用您给定的输入,它返回:

[
  {
    "status": "OK",
    "component": "Service1",
    "status-code": 200
  },
  {
    "status": "OK",
    "component": "Service2",
    "status-code": 200
  }
]

JQ手册(开发) - >基本过滤器

.foo, .foo.bar

最简单的有用过滤器是.foo。当给定JSON对象(又名字典或哈希)作为输入时,它将在键“ foo”处产生值;如果不存在,则返回null。

如果键包含特殊字符,则需要使用双引号将其引起来,例如:."foo$"

从github问题中,如果字段名称带有破折号,则无法选择字段

目前,该解析为减法。当密钥不符合标识符语法时,您始终可以显式使用字符串。

2020-07-27