[英]How to use JQ to merge corresponding elements of two arrays
I have two arrays with the same amount of elements but with different keys/values.我有两个 arrays 具有相同数量的元素但具有不同的键/值。 I want to integrate the key/value of the second array into the first for each index/position.我想将第二个数组的键/值集成到每个索引/位置的第一个数组中。
1.json
[
{
"name": "xxx",
"url": "yyy",
"thumbnail": "nnn"
},
{
"name": "bla bla",
"url": "some-url",
"thumbnail": "another-pic"
}
]
2.json
[
{
"spotifyUrl": "first-spotify-url"
},
{
"spotifyUrl": "second-spotify-url"
}
]
The result I would like to achieve:我想达到的结果:
[
{
"name": "xxx",
"url": "yyy",
"thumbnail": "nnn",
"spotifyUrl": "first-spotify-url"
},
{
"name": "bla bla",
"url": "some-url",
"thumbnail": "another-pic",
"spotifyUrl": "second-spotify-url"
}
]
I already tried different things but couldn't find the result I wanted.我已经尝试过不同的东西,但找不到我想要的结果。 For example this one here:例如这里的这个:
jq -n '
(input | map_values([.])) as $one
| input as $two
| reduce ($two|keys_unsorted[]) as $k2 ( $one;
.[$k2] += [$two[$k2]] )
' 1.json 2.json
is almost what I want, except that the spotify-url is nested into its own object and looks like this:几乎是我想要的,除了 spotify-url 嵌套在它自己的 object 中,看起来像这样:
[
{
"name": "xxx",
"url": "yyy",
"thumbnail": "nnn"
},
{
"spotifyUrl": "first-spotify-url"
}
]
I appreciate any help and bet it's a lot simpler than I can think of.我感谢任何帮助,并打赌它比我想象的要简单得多。 Thanks in advance.提前致谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.