json 如何使用Jolt将嵌套数据转换为线性数据

w41d8nur  于 2023-06-07  发布在  其他
关注(0)|答案(1)|浏览(176)

如何使用jolt将此嵌套数据转换为下面提到的线性格式数据。需要为所有嵌套数据创建单独的条目。每条记录中应包含5个数据practice_loc,prac_num,topId,S1和S2。

数据

[
  {
    "practice_loc": "120",
    "prac_num": "234",
    "topId": "t1",
    "subList": [
      {
        "S1": "A1",
        "S2": "B1"
      },
      {
        "S1": "A2",
        "S2": ""
      }
    ]
  },
  {
    "practice_loc": "334",
    "prac_num": "233",
    "topId": "plumcherry",
    "subList": [
      {
        "SubID1": "A3",
        "SubID2": ""
      }
    ]
  },
  {
    "practice_loc": "987",
    "prac_num": "232",
    "topId": "artica",
    "subList": [
      {
        "SubID1": "A5",
        "SubID2": "B7"
      }
    ]
  }
]

预期数据我尝试了,但没有工作,并给出了null作为结果,请帮助这一点

[
  {
    "practice_loc": "120",
    "prac_num": "234",
    "topId": "t1",
    "S1": "A1",
    "S2": "B1"
  },
  {
    "practice_loc": "120",
    "prac_num": "234",
    "topId": "t1",
    "S1": "A2",
    "S2": ""
  },
  {
    "practice_loc": "334",
    "prac_num": "233",
    "topId": "plumcherry",
    "SubID1": "A3",
    "SubID2": ""
  },
  {
    "practice_loc": "987",
    "prac_num": "232",
    "topId": "artica",
    "SubID1": "A5",
    "SubID2": "B7"
  }
]
xiozqbni

xiozqbni1#

您可以动态地将结果作为

[
  { // group elements by upper level objects wrapper nodes
    "operation": "shift",
    "spec": {
      "*": {
        "subList": {
          "*": {
            "@2|@": "&3_&1"
          }
        }
      }
    }
  },
  { // get rid of the wrappers
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": "[#3].&"
        }
      }
    }
  },
  { // get rid of the extra generated array, namely "subList"
    "operation": "remove",
    "spec": {
      "*": {
        "subList": ""
      }
    }
  }
]

相关问题