Search code examples
jsonjolt

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


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!


Solution

  • 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"
              }
            }
          }
        }
      }
    ]