Jolt 将复杂的嵌套 JSON 处理为平面 JSON

问题描述 投票:0回答:1

当值进入数组时处理嵌套数组的 Jolt 规范

输入

{
  "KUNNR": "123456789",
  "NAME1": "Milchwerke \"Mittelelbe\" GmbH",
  "NAME2": "Milchwerke \"Mittelelbe\" GmbH",
  "NAME3": "Milchwerke \"Mittelelbe\" GmbH",
  "NAME4": "Milchwerke \"Mittelelbe\" GmbH",
  "LAND1": "DE",
  "VKORG": "1255",
  "VTWEG": "00",
  "SPART": "00",
  "PARVW": [
    "WE",
    "WE"
  ],
  "PARZA": [
    "000",
    "000"
  ],
  "KUNN2": [
    "0000045959",
    "0000045959"
  ]
}

预期产出

{
  "KUNNR": "123456789",
  "NAME1": "Milchwerke \"Mittelelbe\" GmbH",
  "NAME2": "Milchwerke \"Mittelelbe\" GmbH",
  "NAME3": "Milchwerke \"Mittelelbe\" GmbH",
  "NAME4": "Milchwerke \"Mittelelbe\" GmbH",
  "LAND1": "DE",
  "VKORG": "1255",
  "VTWEG": "00",
  "SPART": "00",
  "PARVW":  "WE",
  "PARZA": "000",
  "KUNN2": "0000045959"
},
{
  "KUNNR": "123456789",
  "NAME1": "Milchwerke \"Mittelelbe\" GmbH",
  "NAME2": "Milchwerke \"Mittelelbe\" GmbH",
  "NAME3": "Milchwerke \"Mittelelbe\" GmbH",
  "NAME4": "Milchwerke \"Mittelelbe\" GmbH",
  "LAND1": "DE",
  "VKORG": "1255",
  "VTWEG": "00",
  "SPART": "00",
  "PARVW":  "WE",
  "PARZA": "000",
  "KUNN2": "0000045959"
}]

我正在尝试编写一个规范来使用摇动变换进行以下变换。我需要将平面 json 转换为嵌套 Json

我在将平面 JSON 转换为嵌套 JSON 时遇到一些问题。我已经查看了示例,但没有进一步了解上面提到的内容。我需要使用 JOLT 规范来转换 JSON 结构。我使用 https://jolt-demo.appspot.com 来测试以下内容。

json apache-nifi jolt
1个回答
0
投票

您可以使用以下转换:

[
  {
    "operation": "shift",
    "spec": {
      "*": "Others.&", // The elements other than the arrays
      "PARVW|PARZA|KUNN2": "Arrays.&"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Arrays": {
        "*": {
          "*": {
            "@3,Others": { "*": "[&2].&" },
            "@": "[&1].&2"
          }
        }
      }
    }
  },
  { // pick only one per each identical components of the arrays
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": "ONE"
      }
    }
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.