Home > Mobile >  Jolt Transform to get particular value if a condition is met
Jolt Transform to get particular value if a condition is met

Time:03-09

Input json :

{
  "customerPriceBreakUp": [
    {
      "value": 45,
      "componentName": "BASIC_FARE",
      "collectedBy": "VENDOR"
    },
    {
      "value": 0,
      "componentName": "SERVICE_CHARGE",
      "collectedBy": "VENDOR"
    },
    {
      "value": 0,
      "componentName": "SERVICE_TAX",
      "collectedBy": "VENDOR"
    },
    {
      "value": 0,
      "componentName": "VENDOR_INSURANCE_CHARGE",
      "collectedBy": "VENDOR"
    },
    {
      "value": 0,
      "componentName": "TRANSACTION_CHARGE",
      "collectedBy": "XYZ"
    }
  ]
}

Expected Output :

{
  "Base_Fare": 45,
  "Service_Charge": 0,
  "Service_Tax": 0,
  "Vendor_Insurance_Charge": 0,
  "Tranaction _Charge": 0
}

The "CustomerPriceBreakup" is a list of JSONs, and we need the "value" field. if "componentName" = "BASIC_FARE" then take the value field and name it "base_fare" and so on.

CodePudding user response:

You can use two consecutive transformations such as

[
  // prepare future key names to be lowercase
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "*": "=toLower(@(1,&))"
        }
      }
    }
  },
  {
   // match key and value pairs
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "value": "@(1,componentName)"
        }
      }
    }
  }
]

the demo on the site enter image description here

  • Related