gpt4 book ai didi

json - jq 不适用于带有破折号和数字的标签名称

转载 作者:行者123 更新时间:2023-11-29 08:56:50 25 4
gpt4 key购买 nike

我正在使用 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"
}
]
}

有什么想法吗?

最佳答案

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

jq '."component-status"'

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

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

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$".

来自github问题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.

关于json - jq 不适用于带有破折号和数字的标签名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37344329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com