如何使用JOLT转换复杂的JSON嵌套数组?
问题描述:
我试图根据第二个嵌套数组中的值数量将嵌套数组转换为对象.我似乎无法获取值字段的数量并将其用作我的规格中的键.现在,这是我输入的JSON文件:
I'm trying to convert nested arrays into objects depending on the number of values in the second nested array. I can't seem to get the number of the value fields and use that as a key in my spec. Now this is my input JSON file:
{
"meta": {
"regId": "us",
"cId": "SomeProduct",
"weId": 15
},
"data": {
"name": "R",
"details": {
"headers": [
"id",
"cityId",
"cityName"
],
"values": [
[
1539,
17,
"Moskow"
],
[
1539,
17,
"Berlin"
],
[
1539,
17,
"Vienna"
]
]
}
}
}
这是我想要的JSON输出:
This my desired JSON Output:
[
{"regId": "us",
"cId": "SomeProduct",
"weId": 15,
"name":"R",
"id":1539,
"cityId":17,
"cityName":Moskow
},
{"regId": "us",
"cId": "SomeProduct",
"weId": 15,
"name":"R",
"id":1540,
"cityId":11,
"cityName":Berlin
},
{"regId": "us",
"cId": "SomeProduct",
"weId": 15,
"name":"R",
"id":151,
"cityId":18,
"cityName":Vienna
}
]
到目前为止,这是我的规范
This is my spec so far
[
{
"operation": "shift",
"spec": {
"meta": {
"*": "&"
},
"data": {
"name": "&",
"details": {
"values": {
"*": {
"*": "@(3,headers[&])"
}
}
}
}
}
}
]
有人有类似的情况吗?
答
[
{
"operation": "shift",
"spec": {
"data": {
"details": {
"values": {
"*": {
"*": {
// @ takes value of each element and put it to a
// correspondent data2 element ([&2] - go up three levels to
// the first * and takes number of element)
// @(4,headers[&0]) - go up 5 levels and take headers[&0]
// occurence (&0 - go up 1 level - second * and takes number
//of element)
"@": "data2[&2].@(4,headers[&0])"
},
// go up four levels and grab name value and put it into
// data2[&1].name
"@(3,name)": "data2[&1].name",
"@(4,meta)": { // go up five levels and grab meta value
"*": "data2[&2].&" // for each value put it to data2[&2] as it is
}
}
}
}
}
}
},
{
"operation": "shift", // second shift operation to modify result of this above
"spec": {
"data2": "" // removing data2 header
}
}
]