使用Jolt将3个嵌套列表中的数据展平到单个列表中

时间:2018-08-28 07:08:57

标签: json jolt

实际要求是按照所需输出中的描述在每个json对象中获取; parent id。输入包含层次结构中的子级数组。相应的父ID,即如果id = A_B,则其parent_id为A。 震动规格已尝试:

implementation 'com.android.support:recyclerview-v7:28.0.0-rc01'
implementation 'com.android.support:appcompat-v7:28.0.0-rc01'

输入

[{
        "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"
            }
          ]
        }
      ]
    }
  ]
}

1 个答案:

答案 0 :(得分:1)

规格:单独运行每个步骤以查看其作用。

[
  {
    // 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": "[]"
                }
              }
            }
          }
        }
      }
    }
  }]