jq无法使用带破折号和数字的标记名称 [英] jq not working on tag name with dashes and numbers

查看:113
本文介绍了jq无法使用带破折号和数字的标记名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jq,但是在json标记中使用-"会使jq无法编译.我无法逃脱它使其工作.这里的命令

I´m using jq but having "-" in my json tag make jq not compile. I cannot escape it to make it works. Here the command

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

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

I´ve read in the github of jq this post https://github.com/stedolan/jq/issues/202 but I cannot make it works.

这是卷发的输出

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

有什么主意吗?

推荐答案

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

You need to enclose in brackets and double quotes:

jq '.["component-status"]'

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

With your given input it returns:

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

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

.foo, .foo.bar

最简单的有用过滤器是.foo.给定JSON对象(又名 字典或哈希)作为输入,它会在键"foo"处产生值, 或null(如果不存在).

The simplest useful filter is .foo. When given a JSON object (aka dictionary or hash) as input, it produces the value at the key "foo", or null if there’s none present.

如果键包含特殊字符,则需要将其括起来 像这样的双引号:."foo$".

If the key contains special characters, you need to surround it with double quotes like this: ."foo$".

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

From the github issue Cannot select field if field name has dashes:

当前,它被解析为减法.你总是可以 当您的键不符合标识符语法时,显式使用字符串.

Currently, that gets parsed as a subtraction. You can always explicitly use strings for when your keys don't fit identifier syntax.

这篇关于jq无法使用带破折号和数字的标记名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆