gpt4 book ai didi

json - 如何使用jq将两个文件中的数组合并为一个数组?

转载 作者:行者123 更新时间:2023-12-04 23:13:48 33 4
gpt4 key购买 nike

我想合并两个包含 JSON 的文件。它们每个都包含一组 JSON 对象。

注册.json

[
{ "name": "User1", "registration": "2009-04-18T21:55:40Z" },
{ "name": "User2", "registration": "2010-11-17T15:09:43Z" }
]

用户编辑文件
[
{ "name": "User1", "editcount": 164 },
{ "name": "User2", "editcount": 150 },
{ "name": "User3", "editcount": 10 }
]

在理想情况下,我希望合并操作的结果如下:
[
{ "name": "User1", "editcount": 164, "registration": "2009-04-18T21:55:40Z" },
{ "name": "User2", "editcount": 150, "registration": "2010-11-17T15:09:43Z" }
]

我找到了 https://github.com/stedolan/jq/issues/1247#issuecomment-348817802但我明白了
jq: error: module not found: jq

最佳答案

jq 解决方案:

jq -s '[ .[0] + .[1] | group_by(.name)[] 
| select(length > 1) | add ]' registration.json useredits.json

输出:
[
{
"name": "User1",
"registration": "2009-04-18T21:55:40Z",
"editcount": 164
},
{
"name": "User2",
"registration": "2010-11-17T15:09:43Z",
"editcount": 150
}
]

关于json - 如何使用jq将两个文件中的数组合并为一个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49037956/

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