使用输入 JSON 的多种组合来扁平化复杂的嵌套 JSON

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

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

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

输入1 销售区域是列表的情况下,没有嵌套的合作伙伴功能

{
  "CustomerMaster": {
    "Rootnode": {
      "KUNNR": "0006503338",
      "NAME1": "Schwarz Unternehmens Treuhand KG",
      "LAND1": "DK",
      "SalesArea": [
        {
          "VKORG": "1301",
          "VTWEG": "10",
          "SPART": "00"
        },
        {
          "VKORG": "1501",
          "VTWEG": "10",
          "SPART": "00"
        }
      ],
      "Indicator": "",
      "TimeStamp": "2024-12-18T14:04:40Z"
    }
  }
}

预期输出1:

我想要扁平化的 JSON


[  {
  "KUNNR" : "0006503338",
  "NAME1" : "Schwarz Unternehmens Treuhand KG",
  "LAND1" : "DK",
  "Indicator" : "",
  "TimeStamp" : "2024-12-18T14:04:40Z",
  "VKORG" : "1301",
  "VTWEG" : "10",
  "SPART" : "00
}, {
  "KUNNR" : "0006503338",
  "NAME1" : "Schwarz Unternehmens Treuhand KG",
  "LAND1" : "DK",
  "Indicator" : "",
  "TimeStamp" : "2024-12-18T14:04:40Z",
  "VKORG" : "1501",
  "VTWEG" : "10",
  "SPART" : "00",
}
} ]

输入2 销售区域不是列表且一个元素可以包含合作伙伴功能列表的情况


{
  "CustomerMaster": {
    "Rootnode": {
      "KUNNR": "123456789",
      "NAME1": "Milchwerke \"Mittelelbe\" GmbH",
      "NAME2": "Milchwerke \"Mittelelbe\" GmbH",
      "NAME3": "Milchwerke \"Mittelelbe\" GmbH",
      "NAME4": "Milchwerke \"Mittelelbe\" GmbH",
      "LAND1": "DE",
      "SalesArea": {
        "VKORG": "1301",
        "VTWEG": "00",
        "SPART": "00",
        "PartnerFunction": [
          {
            "PARVW": "WE",
            "PARZA": "000",
            "KUNN2": "0000045959"
          },
          {
            "PARVW": "AG",
            "PARZA": "000",
            "KUNN2": "0000047480"
          }
        ]
      }
    }
  }
}

预期输出2:


[ {
  "KUNNR" : "0006503338",
  "NAME1" : "Schwarz Unternehmens Treuhand KG",
  "LAND1" : "DK",
  "Indicator" : "",
  "TimeStamp" : "2024-12-18T14:04:40Z",
  "VKORG" : "1301",
  "VTWEG" : "10",
  "SPART" : "00",
  "PARVW" : "WE",
  "PARZA" : "000",
  "KUNN2" : "0000045959"
}, {
  "KUNNR" : "0006503338",
  "NAME1" : "Schwarz Unternehmens Treuhand KG",
  "LAND1" : "DK",
  "Indicator" : "",
  "TimeStamp" : "2024-12-18T14:04:40Z",
  "VKORG" : "1301",
  "VTWEG" : "10",
  "SPART" : "00",
  "PARVW" : "AG",
  "PARZA" : "000",
  "KUNN2" : "0000047480"
} ]

输入3

销售区域是一个列表,每个元素可以包含合作伙伴功能列表的情况


{
  "CustomerMaster": {
    "Rootnode": {
      "KUNNR": "0006503338",
      "NAME1": "Schwarz Unternehmens Treuhand KG",
      "LAND1": "DK",
      "SalesArea": [
        {
          "VKORG": "1301",
          "VTWEG": "10",
          "SPART": "00",
          "PartnerFunction": [
          {
            "PARVW": "WE",
            "PARZA": "000",
            "KUNN2": "0000045959"
          },
          {
            "PARVW": "AG",
            "PARZA": "000",
            "KUNN2": "0000047480"
          }
        ]
      },
        {
          "VKORG": "1501",
          "VTWEG": "10",
          "SPART": "00",
          "PartnerFunction": [
          {
            "PARVW": "WE",
            "PARZA": "000",
            "KUNN2": "0000045959"
          },
          {
            "PARVW": "AG",
            "PARZA": "000",
            "KUNN2": "0000047480"
          }
        ]
      }
      ],
      "Indicator": "",
      "TimeStamp": "2024-12-18T14:04:40Z"
    }
  }
}

预期输出3:


[
 {
  "KUNNR" : "0006503338",
  "NAME1" : "Schwarz Unternehmens Treuhand KG",
  "LAND1" : "DK",
  "Indicator" : "",
  "TimeStamp" : "2024-12-18T14:04:40Z",
  "VKORG" : "1301",
  "VTWEG" : "10",
  "SPART" : "00",
  "PARVW" : "WE",
  "PARZA" : "000",
  "KUNN2" : "0000045959"
}, {
  "KUNNR" : "0006503338",
  "NAME1" : "Schwarz Unternehmens Treuhand KG",
  "LAND1" : "DK",
  "Indicator" : "",
  "TimeStamp" : "2024-12-18T14:04:40Z",
  "VKORG" : "1301",
  "VTWEG" : "10",
  "SPART" : "00",
  "PARVW" : "AG",
  "PARZA" : "000",
  "KUNN2" : "0000047480"
}, {
  "KUNNR" : "0006503338",
  "NAME1" : "Schwarz Unternehmens Treuhand KG",
  "LAND1" : "DK",
  "Indicator" : "",
  "TimeStamp" : "2024-12-18T14:04:40Z",
  "VKORG" : "1501",
  "VTWEG" : "10",
  "SPART" : "00",
  "PARVW" : "WE1",
  "PARZA" : "000",
  "KUNN2" : "0000045959"
}, {
  "KUNNR" : "0006503338",
  "NAME1" : "Schwarz Unternehmens Treuhand KG",
  "LAND1" : "DK",
  "Indicator" : "",
  "TimeStamp" : "2024-12-18T14:04:40Z",
  "VKORG" : "1501",
  "VTWEG" : "10",
  "SPART" : "00",
  "PARVW" : "AG1",
  "PARZA" : "000",
  "KUNN2" : "0000047480"
} ]

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

您可以尝试使用移位操作将嵌套的 JSON 结构映射到所需的平面结构,同时迭代 SalesArea 列表。以下是适合您的用例的 JOLT 规格: [ { “操作”:“转移”, “规格”:{ “客户大师”:{ “根节点”:{ "KUNNR": "[&1].KUNNR", "NAME1": "[&1].NAME1", "LAND1": "[&1].LAND1", "指标": "[&1].指标", "时间戳": "[&1].时间戳", “销售区域”:{ “”:{ "VKORG": "[&2].[&1].VKORG", "VTWEG": "[&2].[&1].VTWEG", "SPART": "[&2].[&1].SPART" } } } } } }, { “操作”:“转移”, “规格”:{ “”:{ “*”:“” } } } ]

© www.soinside.com 2019 - 2024. All rights reserved.