我想知道是否可以通过查看父数组对象来使用jolt在嵌套数组对象上添加属性
例如,这里是input.json,我想通过查看父所有者数组对象在pet对象中添加一个名为ownerName的元素。
Input.json
{
"owners": [
{
"name": "John",
"ownerId": 1,
"children": [
{
"name": "Jack",
"childId": 11,
"pets": [
{
"type": "dog",
"ownerId": 2
},
{
"type": "cat",
"ownerId": 2
},
{
"type": "dog",
"ownerId": 1
}
]
},
{
"name": "Jill",
"childId": 12,
"pets": [
{
"type": "dog",
"ownerId": 1
},
{
"type": "cat",
"ownerId": 2
},
{
"type": "dog",
"ownerId": 1
}
]
}
]
},
{
"name": "Jane",
"ownerId": 2,
"children": [
{
"name": "jack",
"childId": 21,
"pets": [
{
"type": "rabbit",
"ownerId": 1
},
{
"type": "dog",
"ownerId": 1
},
{
"type": "cat",
"ownerId": 2
}
]
}
]
}
]
}
预期输出
{
"owners": [
{
"name": "John",
"ownerId": 1,
"childs": [
{
"name": "Jack",
"childId": 11,
"pets": [
{
"type": "dog",
"ownerId": 2,
"ownerName": "Jane"
},
{
"type": "cat",
"ownerId": 2,
"ownerName": "Jane"
},
{
"type": "dog",
"ownerId": 1,
"ownerName": "John"
}
]
},
{
"name": "Jill",
"childId": 12,
"pets": [
{
"type": "dog",
"ownerId": 1,
"ownerName": "John"
},
{
"type": "cat",
"ownerId": 2,
"ownerName": "Jane"
},
{
"type": "dog",
"ownerId": 1,
"ownerName": "John"
}
]
}
]
},
{
"name": "Jane",
"ownerId": 2,
"childs": [
{
"name": "jack",
"childId": 21,
"pets": [
{
"type": "rabbit",
"ownerId": 1,
"ownerName": "John"
},
{
"type": "dog",
"ownerId": 1,
"ownerName": "John"
},
{
"type": "cat",
"ownerId": 2,
"ownerName": "Jane"
}
]
}
]
}
]
}
我可以为此编写Jolt规范吗?
2条答案
按热度按时间cetgtptt1#
您可以使用此规范:
我们需要一个类似下面代码的查找来知道我们的1和2所有者名称是什么,我们想使用它们的名称。所以我们想在out spec的第一个
shift
操作中创建一个查找,我们可以在第二个shift
操作中使用它:震动规格:
9ceoxa922#
一个选项将通过将
name
与ownerId
匹配来开始,name
将位于最外层以在下一转换中用作字典,通过再次将它们与ownerId
属性匹配,同时通过使用@1
表达式(例如