英文:
How to add one object into multiple objects -JOLT
问题
[
{
"country": "canada",
"item": "banana",
"price": 3
},
{
"item": "banana",
"price": 3
}
]
英文:
i want to merge the info key data into the rest objects
{
"data": [
{
"item": "banana",
"value": 3
},
{
"item": "banana",
"value": 3
}
],
"info": {
"place": [
{
"country": "canada"
}
]
}
}
my code:
[
{
"operation": "shift",
"spec": {
"data": {
"*": {
"item": "[&1].&",
"value": "[&1].price",
"@2,info.place": {
"*": {
"country": "[&1].&"
}
}
}
}
}
}
]
output:
[
{
"country": [ "canada", "canada" ],
"item": "banana",
"price": 3
},
{
"item": "banana",
"price": 3
}
]
so country should not be an array, of course, i can use cardinality to remove the array but country is not added to the last object.
答案1
得分: 2
你应该跟踪data
数组的索引,而不是place
数组的索引,以便遍历索引 0 和 1,而不仅仅停留在索引 0。
因此,你应该将 [&1]
转换为 [&3]
,放在 "country"
键旁边,如下所示:
[
{
"operation": "shift",
"spec": {
"data": {
"*": {
"item": "[&1].&",
"value": "[&1].price",
"@2,info.place": {
"*": {
"country": "[&3].&"
}
}
}
}
}
}
]
这将产生以下结果:
[
{
"country": "canada",
"item": "banana",
"price": 3
},
{
"country": "canada",
"item": "banana",
"price": 3
}
]
英文:
You should track the index of data
array, not of place
array so that to walk the indexes 0 and 1, to not stay only on the index 0.
So, you should convert [&1]
to [&3]
next to "country"
key such as
[
{
"operation": "shift",
"spec": {
"data": {
"*": {
"item": "[&1].&",
"value": "[&1].price",
"@2,info.place": {
"*": {
"country": "[&3].&"
}
}
}
}
}
}
]
which will yield
[
{
"country": "canada",
"item": "banana",
"price": 3
},
{
"country": "canada",
"item": "banana",
"price": 3
}
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论