Home > Mobile >  JOLT Transformation: Change field value based on the value in a nested element
JOLT Transformation: Change field value based on the value in a nested element

Time:01-13

I have a service that receives a JSON and needs to transform it to a different structure before sending to a third party API. Our team is using JOLT for transformations in the application. I have detailed the current scenario below along with the new ask. The issue with the new ask is that the elements/fields in the resultant JSON have to be derived based on the value of a nested element. I have spent several hours trying different operations (mainly shift and modify-overwrite-beta) but have not been able to find a solution.

Source JSON

{
  "id": "wert23sd-0891-4fcd-ae31-380c0ef61198",
  "topic": "cartsaleOmni/sale",
  "subject": "EventTypeEvent.Cart.Sale",
  "data": {
    "payload": {
      "content": {
        "cartEvent": {
          "eventOccurrenceTime": "2023-01-12T03:09:42.254Z",
          "cartEventId": "fe9c22ca-dc38-4bcd-a220-c7425b9bed7e",
          "eventTriggerTime": "2023-01-12T02:38:43.609Z",
          "eventName": "Sale Cart",
          "cart": {
            "cartId": "be8b22ba-dc38-4bcd-a120-c7425b9bed7e",
            "cartLineItems": [
              {
                "itemId": "44e610ab-c209-4232-8bb4-51f7b9b13a75",
                "cartLineItemId": "fe9c22ca-23ad-46e0-8629-a6593597f183",
                "startsAt": "2023-01-16",
                "endsAt": "2023-01-19",
                "numberOfUnits": 2,
                "clientChannel": "web",
                "clientSource": "mgmri",
                "itemSelectionDetails": {
                  "extSelectionDetails": {
                    "isP1Customer": true
                  }
                },
                "eventType": "saleEvent.44e610ab-c209-4232-8bb4-51f7b9b13a75"
              }
            ]
          }
        }
      }
    }
  },
  "customerId": "CORP11A38249"
}

Current JOLT Spec

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "data": {
        "payload": {
          "content": {
            "cartEvent": {
              "cart": {
                "cartLineItems": {
                  "*": {
                    "eventType": "=concat('carteSaleEvent.', @(1,itemId))"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "customerId": "requests[0].payload.context.CustomerID",
      "data": {
        "payload": {
          "content": {
            "cartEvent": {
              "cart": {
                "cartId": "referenceNumber",
                "cartLineItems": {
                  "*": {
                    "eventType": "requests[0].eventType",
                    "$": "[email protected]",
                    "itemId": "requests[0].payload.context.itemId",
                    "numberOfUnits": "requests[0].payload.context.noOfUnits",
                    "startsAt": "requests[0].payload.context.earliestDate",
                    "endsAt": "requests[0].payload.context.latestDate"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "retryConfigRequestMap": {
        "*": {
          "altEventName": "cartSaleComplete",
          "enabled": true
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "eventType": "Cart.Sale.Complete"
    }
  }
]

Current State Output JSON

{
  "eventType": "Cart.Sale.Complete",
  "referenceNumber": "be8b22ba-dc38-4bcd-a120-c7425b9bed7e",
  "requests": [
    {
      "eventType": "carteSaleEvent.44e610ab-c209-4232-8bb4-51f7b9b13a75",
      "payload": {
        "context": {
          "CustomerID": "CORP11A38249",
          "earliestDate": "2023-01-16",
          "itemId": "44e610ab-c209-4232-8bb4-51f7b9b13a75",
          "latestDate": "2023-01-19",
          "noOfUnits": 2
        }
      }
    }
  ],
  "retryConfigRequestMap": {
    "carteSaleEvent.44e610ab-c209-4232-8bb4-51f7b9b13a75": {
      "altEventName": "cartSaleComplete",
      "enabled": true,
      "maxAttempts": "0"
    }
  }
}

Now I am required to change eventType to cartSaleEvent.PREMIUM in elements in requests array and also in the retryConfigRequestMap if the value of ...itemSelectionDetails.extSelectionDetails.isP1Customer is true. and remain the same if that field is set to 'false' or is not present. So the desired JSON should look like below:

{
  "eventType": "Cart.Sale.Complete",
  "referenceNumber": "be8b22ba-dc38-4bcd-a120-c7425b9bed7e",
  "requests": [
    {
      "eventType": "carteSaleEvent.PREMIUM",
      "payload": {
        "context": {
          "CustomerID": "CORP11A38249",
          "earliestDate": "2023-01-16",
          "itemId": "44e610ab-c209-4232-8bb4-51f7b9b13a75",
          "latestDate": "2023-01-19",
          "noOfUnits": 2
        }
      }
    }
  ],
  "retryConfigRequestMap": {
    "carteSaleEvent.PREMIUM": {
      "altEventName": "cartSaleComplete",
      "enabled": true,
      "maxAttempts": "0"
    }
  }
}

Apologies for the lengthy question. I read through the documentation and also the below links but could not device a working solution. Any help/pointers will be greatly appreciated.

CodePudding user response:

You can rearrange the last shift transformation spec by using a conditional logic through matching "@(0,itemSelectionDetails.extSelectionDetails.isP1Customer)" identifier with true such as

{
  "operation": "shift",
  "spec": {
    "customerId": "requests[0].payload.context.CustomerID",
    "data": {
      "payload": {
        "content": {
          "cartEvent": {
            "cart": {
              "cartId": "referenceNumber",
              "cartLineItems": {
                "*": {
                  "@(0,itemSelectionDetails.extSelectionDetails.isP1Customer)": {
                    "true": {
                      "#carteSaleEvent\\.PREMIUM": "requests[0].eventType",
                      "$2": "retryConfigRequestMap.carteSaleEvent\\.PREMIUM.maxAttempts"
                    },
                    "*": {
                      "@(2,eventType)": "requests[0].eventType",
                      "$2": "retryConfigRequestMap.@(3,eventType).maxAttempts"
                    }
                  },
                  "itemId": "requests[0].payload.context.itemId",
                  "numberOfUnits": "requests[0].payload.context.noOfUnits",
                  "startsAt": "requests[0].payload.context.earliestDate",
                  "endsAt": "requests[0].payload.context.latestDate"
                }
              }
            }
          }
        }
      }
    }
  }
}

where we've gone two levels deeper by using $2,"@(2,eventType)" or "@(3,eventType)"(which needs to traverse one more level as stated on the Right-hand-side)

CodePudding user response:

you can use this spec:

I moved all of your operations to one shift jolt spec, and create a temp variable with this value:

If isP1Customer is true: carteSaleEvent.PREMIUM.

If isP1Customer is false: eventType value.

And finally, you can use the temp value.

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "*": {
                    "eventType": "=concat('carteSaleEvent.', @(1,itemId))"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "customerId": "requests[0].payload.context.CustomerID",
      "#Cart.Sale.Complete": "eventType",
      "data": {
        "*": {
          "*": {
            "*": {
              "cart": {
                "cartId": "referenceNumber",
                "cartLineItems": {
                  "*": {
                    "eventType": "temp",
                    "#test": "requests[&1].test",
                    "$": "[email protected]",
                    "#cartSaleComplete": "[email protected]",
                    "#true": "[email protected]",
                    "itemId": "requests[&1].payload.context.&",
                    "numberOfUnits": "requests[&1].payload.context.noOfUnits",
                    "startsAt": "requests[&1].payload.context.earliestDate",
                    "endsAt": "requests[&1].payload.context.latestDate",
                    "itemSelectionDetails": {
                      "*": {
                        "*": {
                          "true": {
                            "#carteSaleEvent.PREMIUM": "temp"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "temp": "=lastElement"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "eventType": "&",
      "requests": {
        "*": {
          "*": "&2[&1].&",
          "test": {
            "@(4,temp)": "&3[&2].eventType"
          }
        }
      },
      "retryConfigRequestMap": {
        "*": "&1.@(2,temp)"
      }
    }
  }
]
  • Related