Jolt Transform 在满足条件时获得特定值

Jolt Transform to get particular value if a condition is met

输入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"
    }
  ]
}

预期输出:

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

“CustomerPriceBreakup”是一个 JSON 列表,我们需要“值”字段。 如果 "componentName" = "BASIC_FARE" 然后取值字段并将其命名为 "base_fare" 等等。

你可以使用两个连续的转换如

[
  // 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)"
        }
      }
    }
  }
]

网站上的演示http://jolt-demo.appspot.com/