使用jolt转换展平嵌套JSON

11dmarpk  于 2023-05-02  发布在  其他
关注(0)|答案(1)|浏览(176)

我遇到了一个问题,使用jolt转换转换一个非常复杂的嵌套JSON。输入和输出细节如下所示。
输入

[
  {
    "metadata": {
      "event_id": "0635FCAD8AEA1EEDB6868EEFA4D25D34",
      "event_key": "OutboundScheduling_0DE1_040D",
      "event_name": "OutboundScheduling_Update",
      "event_source": "RS4_003",
      "event_time": "2023-04-11T06:45:37.4124Z",
      "event_version": "1.0"
    },
    "payload": {
      "departuresite": "0DE1",
      "shippingpoint": "040D",
      "leadtimes": {
        "transportationlt": [
          {
            "destinationsite": "0DE2",
            "destinationlt": [
              {
                "shippingcondition": "01",
                "destinationsitelt": 2
              },
              {
                "shippingcondition": "03",
                "destinationsitelt": 2
              },
              {
                "shippingcondition": "GR",
                "destinationsitelt": 2
              },
              {
                "shippingcondition": "OP",
                "destinationsitelt": 2
              },
              {
                "shippingcondition": "OS",
                "destinationsitelt": 2
              },
              {
                "shippingcondition": "OX",
                "destinationsitelt": 2
              },
              {
                "shippingcondition": "OY",
                "destinationsitelt": 2
              }
            ]
          }
        ],
        "dncreationleadtimes": [
          {
            "distributionchannel": "70",
            "ordertype": "ZU25",
            "orderreason": "DRP",
            "dncreationlt": 1
          }
        ],
        "dnprocessingleadtimes": [
          {
            "distributionchannel": "70",
            "ordertype": "ZU25",
            "orderreason": "DRP",
            "dnprocessinglt": 1
          }
        ],
        "dnloadingleadtimes": [
          {
            "distributionchannel": "70",
            "shippingcondition": "OU",
            "ordertype": "ZU25",
            "orderreason": "DRP",
            "dnloadinglt": 1
          }
        ],
        "vasleadtimes": []
      }
    }
  }
]

预期输出

[
  {
    "departuresite": "0DE1",
    "shippingpoint": "040D",
    "destinationsite": "0DE2",
    "distributionchannel": "70",
    "ordertype": "ZU25",
    "orderreason": "DRP",
    "dncreationlt": 1,
    "dnloadinglt": 1
  }
]

使用的震动规格

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "payload": {
          "leadtimes": {
            "transportationlt": {
              "*": {
                "destinationlt": {
                  "*": {
                    "@5,departuresite": "&3[#2].departuresite",
                    "@5,shippingpoint": "&3[#2].shippingpoint",
                    "@2,destinationsite": "&3[#2].destinationsite",
                    "@4,dncreationleadtimes[&].distributionchannel": "&3[#2].distributionchannel",
                    "@4,dncreationleadtimes[&].ordertype": "&3[#2].ordertype",
                    "@4,dncreationleadtimes[&].orderreason": "&3[#2].orderreason",
                    "@4,dncreationleadtimes[&].dncreationlt": "&3[#2].dncreationlt",
                    "@4,dnloadingleadtimes[&].dnloadinglt": "&3[#2].dnloadinglt"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  { // get rid of the object keys generated within the previous spec
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

但它并没有像预期的那样到来。
请帮助任何人谁是一个颠簸Maven,帮助我得到所需的输出。我想我卡在最后一步了

ilmyapht

ilmyapht1#

您只需添加如下的shift转换,即可从数组中选择第一个对象

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "payload": {
          "leadtimes": {
            "transportationlt": {
              "*": {
                "destinationlt": {
                  "*": {
                    "@5,departuresite": "[#2].departuresite",
                    "@5,shippingpoint": "[#2].shippingpoint",
                    "@2,destinationsite": "[#2].destinationsite",
                    "@4,dncreationleadtimes[&].distributionchannel": "[#2].distributionchannel",
                    "@4,dncreationleadtimes[&].ordertype": "[#2].ordertype",
                    "@4,dncreationleadtimes[&].orderreason": "[#2].orderreason",
                    "@4,dncreationleadtimes[&].dncreationlt": "[#2].dncreationlt",
                    "@4,dnloadingleadtimes[&].dnloadinglt": "[#2].dnloadinglt"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "0": { // stands for the first index
        "@": "[]" // square-bracketed nest the first object of the array
      }
    }
  }
]

其中[#2]不需要用任何东西限定(* 例如&3不需要 *)
或者直接应用第零索引

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "payload": {
          "leadtimes": {
            "transportationlt": {
              "*": {
                "destinationlt": {
                  "0": { // apply change here only
                    "@5,departuresite": "[#2].departuresite",
                    "@5,shippingpoint": "[#2].shippingpoint",
                    "@2,destinationsite": "[#2].destinationsite",
                    "@4,dncreationleadtimes[&].distributionchannel": "[#2].distributionchannel",
                    "@4,dncreationleadtimes[&].ordertype": "[#2].ordertype",
                    "@4,dncreationleadtimes[&].orderreason": "[#2].orderreason",
                    "@4,dncreationleadtimes[&].dncreationlt": "[#2].dncreationlt",
                    "@4,dnloadingleadtimes[&].dnloadinglt": "[#2].dnloadinglt"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

相关问题