英文:
Jolt - Split objects on the basis of input array
问题
在Jolt中,我想根据地址数组中的项来修改我的对象。对象应该根据数组的项分成多个对象。请协助解决这个问题。谢谢。
预期输出:
[
{
"name": "Mike",
"dept": "IT",
"address": "Austria"
},
{
"name": "Mike",
"dept": "IT",
"address": "India"
},
{
"name": "Mike",
"dept": "IT",
"address": "US"
},
{
"name": "Shelly",
"dept": "Pharma",
"address": "Finland"
},
{
"name": "Shelly",
"dept": "Pharma",
"address": "UK"
}
]
英文:
In Jolt, I want to modify my objects on the basis of items inside the address array. The object should split into the multiple objects on the basis of items of the array. Please assist in solving this. Thankyou.
Input:
[
{
"name": "Mike",
"dept": "IT",
"address": [
"Austria",
"India",
"US"
]
},
{
"name": "Shelly",
"dept": "Pharma",
"address": [
"Finland",
"UK"
]
}
]
Expected output:
[
{
"name": "Mike",
"dept": "IT",
"address": "Austria"
},
{
"name": "Mike",
"dept": "IT",
"address": "India"
},
{
"name": "Mike",
"dept": "IT",
"address": "US"
},
{
"name": "Shelly",
"dept": "Pharma",
"address": "Finland"
},
{
"name": "Shelly",
"dept": "Pharma",
"address": "UK"
}
]
答案1
得分: 2
你可以使用以下的 shift 转换规范,其中返回的属性基于两个分组进行平铺:
- 主数组内对象的索引,
address
数组的索引
例如:
[
{
"operation": "shift",
"spec": {
"*": {
"address": {
"*": {
"@2,name": "&3.&1.name", // 向上遍历 2 级树来获取相关值
"@2,dept": "&3.&1.dept",
"@": "&3.&1.&2"
}
}
}
}
},
{ // 去除对象键
"operation": "shift",
"spec": {
"*": {
"*": ""
}
}
}
]
在网站 http://jolt-demo.appspot.com/ 上的 演示 如下图所示:
英文:
You can use the following shift transformation spec in which the returned attributes are tiled based on the two groupings :
- the indexes of the objects within the main array,
- the indexes of the
address
array
such as
[
{
"operation": "shift",
"spec": {
"*": {
"address": {
"*": {
"@2,name": "&3.&1.name", // go 2 levels up the tree to grab the related value
"@2,dept": "&3.&1.dept",
"@": "&3.&1.&2"
}
}
}
}
},
{ // get rid of object keys
"operation": "shift",
"spec": {
"*": {
"*": ""
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论