使用 Jolt 将来自 3 个嵌套列表的数据展平为一个列表
Flattening data from 3 nested lists, into a single list using Jolt
实际要求是获取每个 json 对象中的父 ID,如所需输出中所述。输入包含层次结构中的子数组。各自的父 ID 即。如果 id = A_B 那么它的 parent_id 应该是 A。
Jolt 规格尝试过:
[{
"operation": "shift",
"spec": {
"children": {
"*": {
"id2": "&",
"name": "&",
"path": "&",
"@": "[&1]",
"@(2,id)": "[&1].parent_id",
"children": {
"*": {
"@": "[&1]",
"@(3,id2)": "[&1].parent_id2"
}
}
}
}
}
}]
#
输入
#
{
"categories": [
{
"id": "A",
"name": "firstName",
"path": "firstPath",
"children": [
{
"id": "A_B",
"name": "secondName",
"path": "secondPath",
"children": [
{
"id": "A_B_C",
"name": "thirdName",
"path": "thirdPath"
}
]
}
]
}
]
}
#
需要此 OUTPUT
#
[{
"id": "A",
"name": "firstName",
"path": "firstPath",
"parentId": "0"
},
{
"id": "A_B",
"name": "secondName",
"path": "secondPath",
"parentId": "A"
},
{
"id": "A_B_C",
"name": "thirdName",
"path": "thirdPath",
"parentId": "A_B"
}]
规范:运行 每个步骤单独查看它在做什么。
[
{
// bootstrap the root level to have "parentId": "0"
"operation": "default",
"spec": {
"categories[]": {
"0": {
"parentId": "0"
}
}
}
},
{
// Build the "data" object you want, but you have to do it
// maintaining the 3 levels of nested lists as the input
// so that the lookups will work.
"operation": "shift",
"spec": {
"categories": {
"*": {
"*": "root[&1].data.&",
"children": {
"*": {
"*": "root[&3].firstLevel[&1].data.&",
"@(2,id)": "root[&3].firstLevel[&1].data.parent_id",
"children": {
"*": {
"*": "root[&5].firstLevel[&3].secondLevel[&1].data.&",
"@(2,id)": "root[&5].firstLevel[&3].secondLevel[&1].data.parent_id"
}
}
}
}
}
}
}
},
{
// Lastly, accumulate all the finished "data" elements from the
// 3 nested arrays into a single top level array.
"operation": "shift",
"spec": {
"root": {
"*": {
"data": "[]",
"firstLevel": {
"*": {
"data": "[]",
"secondLevel": {
"*": {
"data": "[]"
}
}
}
}
}
}
}
}]
实际要求是获取每个 json 对象中的父 ID,如所需输出中所述。输入包含层次结构中的子数组。各自的父 ID 即。如果 id = A_B 那么它的 parent_id 应该是 A。 Jolt 规格尝试过:
[{
"operation": "shift",
"spec": {
"children": {
"*": {
"id2": "&",
"name": "&",
"path": "&",
"@": "[&1]",
"@(2,id)": "[&1].parent_id",
"children": {
"*": {
"@": "[&1]",
"@(3,id2)": "[&1].parent_id2"
}
}
}
}
}
}]
#
输入
#{
"categories": [
{
"id": "A",
"name": "firstName",
"path": "firstPath",
"children": [
{
"id": "A_B",
"name": "secondName",
"path": "secondPath",
"children": [
{
"id": "A_B_C",
"name": "thirdName",
"path": "thirdPath"
}
]
}
]
}
]
}
#
需要此 OUTPUT
#[{
"id": "A",
"name": "firstName",
"path": "firstPath",
"parentId": "0"
},
{
"id": "A_B",
"name": "secondName",
"path": "secondPath",
"parentId": "A"
},
{
"id": "A_B_C",
"name": "thirdName",
"path": "thirdPath",
"parentId": "A_B"
}]
规范:运行 每个步骤单独查看它在做什么。
[
{
// bootstrap the root level to have "parentId": "0"
"operation": "default",
"spec": {
"categories[]": {
"0": {
"parentId": "0"
}
}
}
},
{
// Build the "data" object you want, but you have to do it
// maintaining the 3 levels of nested lists as the input
// so that the lookups will work.
"operation": "shift",
"spec": {
"categories": {
"*": {
"*": "root[&1].data.&",
"children": {
"*": {
"*": "root[&3].firstLevel[&1].data.&",
"@(2,id)": "root[&3].firstLevel[&1].data.parent_id",
"children": {
"*": {
"*": "root[&5].firstLevel[&3].secondLevel[&1].data.&",
"@(2,id)": "root[&5].firstLevel[&3].secondLevel[&1].data.parent_id"
}
}
}
}
}
}
}
},
{
// Lastly, accumulate all the finished "data" elements from the
// 3 nested arrays into a single top level array.
"operation": "shift",
"spec": {
"root": {
"*": {
"data": "[]",
"firstLevel": {
"*": {
"data": "[]",
"secondLevel": {
"*": {
"data": "[]"
}
}
}
}
}
}
}
}]