JQ-打印特定键值对

发布于 2025-01-10 16:32:20 字数 812 浏览 0 评论 0原文

我有这个 JSON:

{
    "time": "2022-02-28T22:00:55.196Z",
    "severity": "INFO",
    "params": [
        {"key": "state", "value": "pending"},
        {"key": "options", "value": "request"},
        {"key": "description", "value": "[FILTERED]"}
    ],
    "content_length": "231"
}

我想打印键与状态和选项以及时间及其值匹配的键值对。我可以使用以下命令打印时间和所有键值对,但不确定如何提取特定的键值对。

jq '"time:\(.time)" ,[.params[] | "key:\(.key)" ,"value:\(.value)"]' test.json

这给出了输出:

"time:2022-02-28T22:00:55.196Z"
[
  "key:state",
  "value:pending",
  "key:options",
  "value:request",
  "key:description",
  "value:[FILTERED]"
]

但我想要的输出是:

"time:2022-02-28T22:00:55.196Z"
  "key:state",
  "value:pending",
  "key:options",
  "value:request"

I have this JSON:

{
    "time": "2022-02-28T22:00:55.196Z",
    "severity": "INFO",
    "params": [
        {"key": "state", "value": "pending"},
        {"key": "options", "value": "request"},
        {"key": "description", "value": "[FILTERED]"}
    ],
    "content_length": "231"
}

I want to print key value pairs of where the key matches to state and options and also the time and its value. I am able to print the time and all key value pairs by using below command, but not sure how to extract specific key value pairs.

jq '"time:\(.time)" ,[.params[] | "key:\(.key)" ,"value:\(.value)"]' test.json

This gives the output:

"time:2022-02-28T22:00:55.196Z"
[
  "key:state",
  "value:pending",
  "key:options",
  "value:request",
  "key:description",
  "value:[FILTERED]"
]

But my desired output is:

"time:2022-02-28T22:00:55.196Z"
  "key:state",
  "value:pending",
  "key:options",
  "value:request"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咽泪装欢 2025-01-17 16:32:20

上述问题的一种解决方案是:

< test.json jq ' 
  "time:\(.time)",
  [.params[] | select(.key|IN("state","options"))
   | "key:\(.key)" ,"value:\(.value)"]
' | sed '/^[][]$/d'

但是,几乎可以肯定的是,稍微修改一下要求,使输出格式不那么特殊。这也将使制定更清晰的(例如 only-jq)解决方案变得更加容易。

One solution to the stated problem would be:

< test.json jq ' 
  "time:\(.time)",
  [.params[] | select(.key|IN("state","options"))
   | "key:\(.key)" ,"value:\(.value)"]
' | sed '/^[][]$/d'

However, it would almost certainly be better to modify the requirements slightly so that the output format is less idiosyncratic. This should also make it easier to formulate a cleaner (e.g. only-jq) solution.

星光不落少年眉 2025-01-17 16:32:20

您可以使用@csv(逗号分隔值)。

过滤器

"time:\(.time)", 
(.params | 
[map(select(.key=="state" or .key=="options"))[] 
| "key:\(.key)", "value:\(.value)"] 
| @csv)

输入

{
    "time": "2022-02-28T22:00:55.196Z",
    "severity": "INFO",
    "params": [
        {"key": "state", "value": "pending"},
        {"key": "options", "value": "request"},
        {"key": "description", "value": "[FILTERED]"}
    ],
    "content_length": "231"
}

输出

time:2022-02-28T22:00:55.196Z
"key:state","value:pending","key:options","value:request"

演示

https://jqplay.org/s/F_3QP6-EvK

You can use @csv (comma separated values).

Filter

"time:\(.time)", 
(.params | 
[map(select(.key=="state" or .key=="options"))[] 
| "key:\(.key)", "value:\(.value)"] 
| @csv)

Input

{
    "time": "2022-02-28T22:00:55.196Z",
    "severity": "INFO",
    "params": [
        {"key": "state", "value": "pending"},
        {"key": "options", "value": "request"},
        {"key": "description", "value": "[FILTERED]"}
    ],
    "content_length": "231"
}

Output

time:2022-02-28T22:00:55.196Z
"key:state","value:pending","key:options","value:request"

Demo

https://jqplay.org/s/F_3QP6-EvK

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文