使用jq合并具有公共ID的密钥 [英] Use jq to merge keys with common id
问题描述
考虑文件"b.json":
consider a file 'b.json':
[
{
"id": 3,
"foo": "cannot be replaced, id isn't in a.json, stay untouched",
"baz": "do not touch3"
},
{
"id": 2,
"foo": "should be replaced with 'foo new2'",
"baz": "do not touch2"
}
]
和'a.json':
[
{
"id": 2,
"foo": "foo new2",
"baz": "don't care"
}
]
我想使用jq和来自a.json的匹配值来更新b.json中的键"foo".它也应该与a.json中的多个条目一起使用.
I want to update the key "foo" in b.json using jq with the matching value from a.json. It should also work with more than one entry in a.json.
因此所需的输出是:
[
{
"id": 3,
"foo": "cannot be replaced, id isn't in a.json, stay untouched",
"baz": "do not touch3"
},
{
"id": 2,
"foo": "foo new2",
"baz": "do not touch2"
}
]
推荐答案
以下是使用INDEX/2
的几种可能性之一.如果您的jq没有内置此功能,请参见下文.
Here's one of several possibilities that use INDEX/2
. If your jq does not have this as a built-in, see below.
jq --argfile a a.json '
INDEX($a[]; .id) as $dict
| map( (.id|tostring) as $id
| if ($dict|has($id)) then .foo = $dict[$id].foo
else . end)' b.json
还有其他方法可以传递a.json和b.json的内容.
There are other ways to pass in the contents of a.json and b.json.
上面对INDEX的使用假定不存在冲突",例如,如果其中一个对象的.id等于1,而另一个对象的.id等于"1",则将发生冲突".如果有可能发生这种冲突,则可以使用更复杂的INDEX定义.
The above use of INDEX assumes there are no "collisions", which would happen if, for example, one of the objects has .id equal to 1 and another has .id equal to "1". If there is a possibility of such a collision, then a more complex definition of INDEX could be used.
直接从Builtin.jq:
Straight from builtin.jq:
def INDEX(stream; idx_expr):
reduce stream as $row ({}; .[$row|idx_expr|tostring] = $row);
这篇关于使用jq合并具有公共ID的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!