[英]Flattening data from 3 nested lists, into a single list using Jolt
實際要求是在每個 json 對象中獲取 ;parent id,如所需輸出中所述。 輸入包含層次結構中的子項數組。 相應的父 ID,即。 如果 id = A_B 那么它的 parent_id 應該是 A。 Jolt Spec 嘗試過:
[{
"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" } ] } ] } ] }
#
需要這個輸出
# [{ "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": "[]"
}
}
}
}
}
}
}
}]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.