Flat JSON到3个公共字段上的嵌套JSON

jaxagkaj  于 2023-03-24  发布在  其他
关注(0)|答案(1)|浏览(138)

我遇到了一个问题,使用jolt转换将平面JSON转换为嵌套JSON。
数据需要按照**DOC_TYPEVENDORPLANT**键嵌套
输入

[
  {
    "DOC_TYPE": "ZU15",
    "VENDOR": "0DE1",
    "PLANT": "0DEA",
    "REF_1": "AR1000000061",
    "RSD": "20230405",
    "SALES_CHANNEL": "20",
    "DELIVERY_PRIORITY": "30",
    "ORDER_REASON": "RRU",
    "PO_ITEM": "00010",
    "MATERIAL": "AJ5563310",
    "QUANTITY": "1",
    "GR_PROCESSING": "2",
    "HEADER_TEXT": "Test"
  },
  {
    "DOC_TYPE": "ZU15",
    "VENDOR": "0DE1",
    "PLANT": "0DEA",
    "REF_1": "AR1000000062",
    "RSD": "20230405",
    "SALES_CHANNEL": "20",
    "DELIVERY_PRIORITY": "30",
    "ORDER_REASON": "RRU",
    "PO_ITEM": "00020",
    "MATERIAL": "AC8791590",
    "QUANTITY": "3",
    "GR_PROCESSING": "1",
    "HEADER_TEXT": "Test"
  },
  {
    "DOC_TYPE": "ZU20",
    "VENDOR": "0DE1",
    "PLANT": "0DEA",
    "REF_1": "AR1000000063",
    "RSD": "20230405",
    "SALES_CHANNEL": "20",
    "DELIVERY_PRIORITY": "30",
    "ORDER_REASON": "RRU",
    "PO_ITEM": "00030",
    "MATERIAL": "W27778506",
    "QUANTITY": "4",
    "GR_PROCESSING": "3",
    "HEADER_TEXT": "Test"
  },
  {
    "DOC_TYPE": "ZU20",
    "VENDOR": "0DE1",
    "PLANT": "0DEA",
    "REF_1": "AR1000000064",
    "RSD": "20230405",
    "SALES_CHANNEL": "20",
    "DELIVERY_PRIORITY": "30",
    "ORDER_REASON": "RRU",
    "PO_ITEM": "00040",
    "MATERIAL": "GL8981210",
    "QUANTITY": "2",
    "GR_PROCESSING": "4",
    "HEADER_TEXT": "Test"
  },
  {
    "DOC_TYPE": "ZU25",
    "VENDOR": "0D97",
    "PLANT": "0D99",
    "REF_1": "AR1000000065",
    "RSD": "20230406",
    "SALES_CHANNEL": "20",
    "DELIVERY_PRIORITY": "30",
    "ORDER_REASON": "RRU",
    "PO_ITEM": "00070",
    "MATERIAL": "AJ5563310",
    "QUANTITY": "2",
    "GR_PROCESSING": "",
    "HEADER_TEXT": ""
  }
]

我写了jolt规格,我没有得到所需的输出
震动规范

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "DOC_TYPE": "@(1,DOC_TYPE).&",
        "VENDOR": "@(1,DOC_TYPE).&",
        "REF_1": "@(1,DOC_TYPE).&",
        "PLANT": "@(1,DOC_TYPE).&",
        "RSD": "@(1,DOC_TYPE).&",
        "SALES_CHANNEL": "@(1,DOC_TYPE).&",
        "DELIVERY_PRIORITY": "@(1,DOC_TYPE).&",
        "ORDER_REASON": "@(1,DOC_TYPE).&",
        "HEADER_TEXT": "@(1,DOC_TYPE).&",
        "*": "@(1,DOC_TYPE).Items[&1].&"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "DOC_TYPE": "ONE",
        "VENDOR": "ONE",
        "REF_1": "ONE",
        "PLANT": "ONE",
        "RSD": "ONE",
        "SALES_CHANNEL": "ONE",
        "DELIVERY_PRIORITY": "ONE",
        "ORDER_REASON": "ONE",
        "HEADER_TEXT": "ONE"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

所需输出

[
  {
    "DOC_TYPE": "ZU15",
    "VENDOR": "0DE1",
    "PLANT": "0DEA",
    "REF_1": "AR1000000061",
    "RSD": "20230405",
    "SALES_CHANNEL": "20",
    "DELIVERY_PRIORITY": "30",
    "ORDER_REASON": "RRU",
    "Items": [
      {
        "PO_ITEM": "00010",
        "MATERIAL": "AJ5563310",
        "QUANTITY": "1",
        "GR_PROCESSING": "2"
      },
      {
        "PO_ITEM": "00020",
        "MATERIAL": "AC8791590",
        "QUANTITY": "3",
        "GR_PROCESSING": "1"
      }
    ]
  },
  {
    "DOC_TYPE": "ZU20",
    "VENDOR": "0DE1",
    "PLANT": "0DEA",
    "REF_1": "AR1000000061",
    "RSD": "20230405",
    "SALES_CHANNEL": "20",
    "DELIVERY_PRIORITY": "30",
    "ORDER_REASON": "RRU",
    "Items": [
      {
        "PO_ITEM": "00030",
        "MATERIAL": "W27778506",
        "QUANTITY": "4",
        "GR_PROCESSING": "3"
      },
      {
        "PO_ITEM": "00040",
        "MATERIAL": "GL8981210",
        "QUANTITY": "2",
        "GR_PROCESSING": "4"
      }
    ]
  },
  {
    "DOC_TYPE": "ZU25",
    "VENDOR": "0DE1",
    "PLANT": "0DEA",
    "REF_1": "AR1000000061",
    "RSD": "20230405",
    "SALES_CHANNEL": "20",
    "DELIVERY_PRIORITY": "30",
    "ORDER_REASON": "RRU",
    "Items": [
      {
        "PO_ITEM": "00070",
        "MATERIAL": "AJ5563310",
        "QUANTITY": "4",
        "GR_PROCESSING": "3"
      }
    ]
  }
]

现在它没有正确地到来。
谁是震动Maven,可以帮助我得到所需的输出。我想我是卡在最后一步

zujrkrfu

zujrkrfu1#

您可以使用以下转换,它将在分组为独立对象后,仅从数组中选择除"Items"之外的第一个元素

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "@(1,DOC_TYPE).@(1,VENDOR).@(1,PLANT).&",
        "PO_ITEM|MATERIAL|QUANTITY|GR_PROCESSING": "@(1,DOC_TYPE).@(1,VENDOR).@(1,PLANT).Items[&1].&"
      }
    }
  },
  { // get rid of object keys
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "@": ""
          }
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": "ONE",
        "Items": "MANY"
      }
    }
  },
  {// get rid of the "HEADER_TEXT" attribute 
    "operation": "remove",
    "spec": {
      "*": {
        "HEADER_TEXT": ""
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  }
]

相关问题