抱歉问了个菜鸟问题。我需要合并两个JSON。我不太确定jq中使用什么操作/函数,所以请启发我。谢谢你,谢谢
color.json
[{"id":1,"color":"red"},{"id":2,"color":"green"},{"id":3,"color":"blue"}]
字符串
shape.json
[
{
"shape": "square",
"color": {
"id": 1,
"name": "red"
},
"texture": "smooth"
},
{
"shape": "circle",
"color": {
"id": 2,
"name": "green"
},
"texture": "smooth"
},
{
"shape": "triangle",
"color": {
"id": 3,
"name": "blue"
},
"texture": "smooth"
},
{
"shape": "triangle",
"color": {
"id": 3,
"name": "blue"
},
"texture": "rough"
}
]
型
所需的输出
[
{
"id": 1,
"color": "red",
"shapes": [
{
"shape": "square",
"texture": "smooth"
}
]
},
{
"id": 2,
"color": "green",
"shapes": [
{
"shape": "circle",
"texture": "smooth"
}
]
},
{
"id": 3,
"color": "blue",
"shapes": [
{
"shape": "triangle",
"texture": "smooth"
},
{
"shape": "triangle",
"texture": "rough"
}
]
}
]
型
我用jq 'JOIN(INDEX(inputs[];.id);.[];.color.id | tostring;add)' shape.json color.json
。但这并不是我的目标。
2条答案
按热度按时间rkkpypqq1#
下面是一个基于
reduce
的方法,它简单地迭代shape.json
项,将它们从colors.json
添加到INDEX
ed对象。最后一个map(.)
将对象重新转换为数组:个字符
Demo
rekjcdws2#
看起来你可以通过使用
shape.json
来获得所需的输出。你可以使用这个(可能没有优化)过滤器来获得所需的输出:
字符串
给予:
型
Online demo