英文:
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
- If one of the elements of the input JSON has the value
DISTRIBUTION_CENTER
forfacilityTypeId
, then only prepare theorder ID
andshipment ID
. - But if the elements don't have the value
DISTRIBUTION_CENTER
forfacilityTypeId
, then prepare the shippingAmount
, from where theorderAdjustmentTypeId
=SHIPPING_CHARGES
, along with theorder ID
andshipment 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"
}
}
}
}
}
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论