如何使用JQ将对象数组转换为钥匙值的分开列表 - 输出特定的键值而不是数组索引

发布于 2025-01-28 08:49:41 字数 582 浏览 1 评论 0原文

这与我以前的问题有关: 如何使用JQ将对象的格式数组进行格式化,以分开关键值的列表

我如何(通常)使用JQ(通常)将下面的输入文件转换为下面的输出文件。键“ ID”的值Uniqely标识数组元素。输出文件的记录格式为:(键“ id”的值)|键|价值。

如果我对以前的问题的解决方案添加尴尬,我可以做到这一点,但是我很难在JQ中解决所有问题。

输入文件:

[{"id": 11, "b": 100},
 {"id": 12, "d": "fred", "e": 300}]

输出文件:

11|id|11
11|b|100
12|id|12
12|d|fred
12|e|300

This is related to my previous question:
How to use jq to format array of objects to separated list of key values

How can I (generically) transform the input file below to the output file below, using jq. The value at key "id" uniqely identifies the array element. The record format of the output file is: (value at key "id") | key | value.

I can do this if I add awk to solution of previous question, but I am having trouble getting my head around doing it all in jq.

Input file:

[{"id": 11, "b": 100},
 {"id": 12, "d": "fred", "e": 300}]

Output File:

11|id|11
11|b|100
12|id|12
12|d|fred
12|e|300

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

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

发布评论

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

评论(1

呆橘 2025-02-04 08:49:41

这是使用to_entries的解决方案,将对象分解为键值对数组:

jq -r '.[] | .id as $id | to_entries[] | [$id,.key,.value] | join("|")'
11|id|11
11|b|100
12|id|12
12|d|fred
12|e|300

Here's a solution using to_entries, which decomposes an object into an array of key-value pairs:

jq -r '.[] | .id as $id | to_entries[] | [$id,.key,.value] | join("|")'
11|id|11
11|b|100
12|id|12
12|d|fred
12|e|300
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文