jq 不处理带有破折号和数字的标签名称 [英] jq not working on tag name with dashes and numbers

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

问题描述

我正在使用 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的输出

<代码> {"状态": "好的","主机名": "0b0b495a46db",组件状态":[{状态代码":200,组件":服务1",状态":正常"},{状态代码":200,组件":Service2",状态":正常"}]}

有什么想法吗?

解决方案

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

jq '."component-status"'

根据您给定的输入,它返回:

<预><代码>[{"状态": "好的",组件":服务1",状态代码":200},{"状态": "好的",组件":Service2",状态代码":200}]

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

<块引用>

.foo, .foo.bar

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

如果key包含特殊字符,需要用像这样的双引号:."foo$".

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

<块引用>

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

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[]'

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

This is the output of the curl

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

Any idea?

解决方案

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
  }
]

The jq Manual (development) --> Basic filters:

.foo, .foo.bar

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.

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

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天全站免登陆