过滤输入的JSON数据,根据数值准备期望的输出。

huangapple go评论59阅读模式
英文:

Filter out the input JSON based on the values and prepare the expected output using it

问题

[
  {
    "Order_ID": "16487",
    "Shipment_ID": "0001"
  },
  {
    "Order_ID": "16488",
    "Amount": 25.95,
    "Shipment_ID": "0001"
  },
  {
    "Order_ID": "16489",
    "Shipment_ID": "0001"
  }
]
英文:

Requirement: Prepare the value for shipping amount field from the input JSON based on certain conditions, like below

  1. If one of the elements of the input JSON has the value DISTRIBUTION_CENTER for facilityTypeId, then only prepare the order ID and shipment ID.
  2. But if the elements don't have the value DISTRIBUTION_CENTER for facilityTypeId, then prepare the shipping Amount, from where the orderAdjustmentTypeId = SHIPPING_CHARGES, along with the order ID and shipment ID.

Input JSON

[
  {
    "orderId": "16487",
    "orderAdjustments": [
      {
        "amount": 0,
        "orderAdjustmentTypeId": "DONATION_ADJUSTMENT"
      },
      {
        "amount": 15.95,
        "orderAdjustmentTypeId": "SHIPPING_CHARGES"
      }
    ],
    "shipments": [
      {
        "shipmentId": "0001",
        "shipmentItems": [
          {
            "parentFacilityTypeId": "PHYSICAL_STORE",
            "quantity": 1
          },
          {
            "parentFacilityTypeId": "DISTRIBUTION_CENTER",
            "quantity": 1
          }
        ]
      }
    ]
  },
  {
    "orderId": "16488",
    "orderAdjustments": [
      {
        "amount": 10,
        "orderAdjustmentTypeId": "DONATION_ADJUSTMENT"
      },
      {
        "amount": 25.95,
        "orderAdjustmentTypeId": "SHIPPING_CHARGES"
      }
    ],
    "shipments": [
      {
        "shipmentId": "0001",
        "shipmentItems": [
          {
            "parentFacilityTypeId": "PHYSICAL_STORE",
            "quantity": 1
          }
        ]
      }
    ]
  },
  {
    "orderId": "16489",
    "orderAdjustments": [
      {
        "amount": 10,
        "orderAdjustmentTypeId": "DONATION_ADJUSTMENT"
      },
      {
        "amount": 25.95,
        "orderAdjustmentTypeId": "SHIPPING_CHARGES"
      }
    ],
    "shipments": [
      {
        "shipmentId": "0001",
        "shipmentItems": [
          {
            "parentFacilityTypeId": "DISTRIBUTION_CENTER",
            "quantity": 1
          }
        ]
      }
    ]
  }
]

Expected JSON

[
  {
    "Order_ID": "16487",
    "Shipment_ID": "0001"
  },
  {
    "Order_ID": "16488",
    "Amount": 25.95,
    "Shipment_ID": "0001"
  },
  {
    "Order_ID": "16489",
    "Shipment_ID": "0001"
  }
]

I have prepare the Jolt spec as per the requirements mentioned above.

Jolt Spec

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "shipments": {
          "*": {
            "shipmentItems": {
              "*": {
                "parentFacilityTypeId": {
                  "DISTRIBUTION_CENTER": {
                    "#Y": "[&7].fulfilledFromWH"
                  }
                }
              }
            }
          }
        },
        "@": "[&]"
      }
    }
  },
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "fulfilledFromWH": "N"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "fulfilledFromWH": {
          "N": {
            "@(2,orderAdjustments)": {
              "*": {
                "orderAdjustmentTypeId": {
                  "SHIPPING_CHARGES": {
                    "@(2,amount)": "[&7].shippingAmount"
                  }
                }
              }
            }
          }
        },
        "@": "[&]"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "orderId": "[&1].Order_ID",
        "shippingAmount": "[&1].Amount",
        "shipments": {
          "*": {
            "shipmentId": "[&3].Shipment_ID"
          }
        }
      }
    }
  }
]

I got the desired output by using the above spec, but need to know if there is another approach to achieve the expected output.

Any help would be appreciated!

答案1

得分: 1

另一种选项是检查**"DISTRIBUTION_CENTER"值在修改**转换中的存在,例如

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "orderId": "@(1,orderId).&",
        "@shipments[0].shipmentId": "@(1,orderId).Shipment_ID",
        "*": { // level of both "orderAdjustments" and "shipments"
          "*": {
            "amount": "@(3,orderId).@(1,orderAdjustmentTypeId).&",
            "shipmentItems": {
              "*": {
                "parentFacilityTypeId": {
                  "DISTRIBUTION_CENTER": { "# ": "@(7,orderId).Amountt" } // a new parameter defined
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "~Amountt": "@(1,SHIPPING_CHARGES.amount)", // means if "Amount" does not exist or is null, then set it with the desired value 
        "Amount": ["=toDouble(@(1,Amountt))", null]
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "orderId|Sh*": "[#2].&",
        "Amount": {
          "*": { // the null values vanish through this loop
            "@1": "[#4].&2"
          }
        }
      }
    }
  }
]
英文:

Another option would be checking out the existence of "DISTRIBUTION_CENTER" value within a modify transformation such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "orderId": "@(1,orderId).&",
        "@shipments[0].shipmentId": "@(1,orderId).Shipment_ID",
        "*": { // level of both "orderAdjustments" and "shipments"
          "*": {
            "amount": "@(3,orderId).@(1,orderAdjustmentTypeId).&",
            "shipmentItems": {
              "*": {
                "parentFacilityTypeId": {
                  "DISTRIBUTION_CENTER": { "# ": "@(7,orderId).Amountt" } // a new parameter defined
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "~Amountt": "@(1,SHIPPING_CHARGES.amount)", // means if "Amount" does not exist or is null, then set it with the desired value 
        "Amount": ["=toDouble(@(1,Amountt))", null]
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "orderId|Sh*": "[#2].&",
        "Amount": {
          "*": { // the null values vanish through this loop
            "@1": "[#4].&2"
          }
        }
      }
    }
  }
]

huangapple
  • 本文由 发表于 2023年6月29日 19:03:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定