Sorry for a noob question. I got two json that I need to merge. And I’m not really sure what operation/function to use in jq, so please enlighten me. Thanks!
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"
}
]
Desired output
[
{
"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"
}
]
}
]
I tried using jq 'JOIN(INDEX(inputs[];.id);.[];.color.id | tostring;add)' shape.json color.json
. But it’s not exactly what I’m aiming for.
2
Answers
Seem like you can get the desired output by just using the
shape.json
.You could this (probably not optimised) filter to get to the desired output:
GIves:
Online demo
Here’s a
reduce
-based approach which simply iterates over theshape.json
items, adding them to theINDEX
ed object fromcolors.json
. A finalmap(.)
re-converts the object into an array:Demo