Home > front end >  Jolt transformation depending on field
Jolt transformation depending on field

Time:01-06

I have tried the following transformation spec :

[
  {
    "operation": "shift",
    "spec": {
      "response": {
        "success": {
          "true": {
            "#false": "error",
            "@(2,message)": "info_message",
            "series": "body.offer_series",
            "number": "body.offer_number",
            "end_date": "body.policy_end_date",
            "expiry_date": "body.offer_expiration_date",
            "commission": "body.commission.commission_amount",
            "premium_net": "body.premium.net_premium.amount",
            "total_premium": "body.premium.amount",
            "installments": {
              "*": {
                "number": "body.premium.installments[&1].number",
                "value": "body.premium.installments[&1].amount",
                "currency": "body.premium.installments[&1].currency",
                "due_date": "body.premium.installments[&1].due_date"
              }
            }
          },
          "false": {
            "*": {
              "*": "body.error_message"
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "body": {
        "good_id": "=toInteger"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "error_message": "=join(' | ',@(1,&))"
      }
    }
  }
]

in order to extract some fields from the request and the rest from the response. But no fields are displayed except the message and error. how should i write the spec in order to display all the fields depending the on the success of the call.

with the input for success = true :

{
  "response": {
    "success": true,
    "data": {
      "series": "XX/32/V32/LM",
      "number": 8015,
      "end_date": "14/01/2024",
      "premium": 10192.05,
      "premium_net": 8969,
      "commission": 12,
      "installments": [
        {
          "number": 1,
          "value": 11007.41,
          "currency": "RON",
          "due_date": "05/01/2023"
        }
      ],
      "total_premium": 11007.41,
      "reference_premium": 1994,
      "direct_settlement_cover": 1,
      "direct_settlement": 815.36,
      "direct_settlement_net": 717.52,
      "bm": "B0",
      "exclusion_countries": [
        "BY",
        "IL",
        "IR",
        "MA",
        "RUS",
        "TN",
        "UA"
      ],
      "expiry_date": "04/02/2023",
      "appendix": false
    },
    "message": "Oferta a fost emisă cu succes"
  }
}

i would like to have the following output

{
  "error" : "false",
  "body" : {
    "offer_series" : "XX/32/V32/LM",
    "offer_number" : 8015,
    "policy_end_date" : "14/01/2024",
    "offer_expiration_date" : "04/02/2023",
    "commission" : {
      "commission_amount" : 12
    },
    "premium" : {
      "net_premium" : {
        "amount" : 8969
      },
      "amount" : 11007.41,
      "installments" : [ {
        "number" : 1,
        "amount" : 11007.41,
        "currency" : "RON",
        "due_date" : "05/01/2023"
      } ]
    }
  }
}

and for false the following :

input

{
  "response": {
    "success": false,
    "message": "Eroare validare date",
    "data": {
      "driver.0.name": [
        "Trebuie să menționați nume/denumire pentru șofer"
      ],
      "driver.1.name": [
        "Trebuie să menționați nume/denumire pentru șofer"
      ],
      "driver.1tin": [
        "CNP/CUI invalid pentru șofer"
      ],
      "vehicle.card": [
        "Serie CIV invalidă pentru vehiculul asigurat"
      ]
    }
  }
}

desired output

{
  "body" : {
    "info_message" : "Eroare validare date",
    "error_message" : "Trebuie să menționați nume/denumire pentru șofer | Trebuie să menționați nume/denumire pentru șofer | CNP/CUI invalid pentru șofer | Serie CIV invalidă pentru vehiculul asigurat"
  }
}

CodePudding user response:

You can conditionally match the respective values of success within a shift transformation spec such as

[
  { 
    "operation": "shift",
    "spec": {
      "response": {
        "success": {
          "true": {
            "#false": "error",
            "@(2,data)": { //need to go two levels up the tree
              "series|number": "body.offer_&", // the "pipe" is the OR operator
              "end_date": "body.policy_&",
              "expiry_date": "body.offer_expiration_date",
              "comm*": "body.&.&_amount", // replicates the expression "commission" twice
              "*um_net": "body.&(0,1)um.net_&(0,1)um.amount", // &(0,1) -> replacement for the expression "premi" at the current level("0") of the first("1") asterisk
              "total_premium": "amount",
              "ins*": "body.&"
            }
          },
          "false": {
            // "@1": "&2",
            "@(2,message)": "body.info_message",
            "@(2,data)": {
              "*": {
                "*": "body.error_message"
              }
            }
          }
        }
      }
    }
  },
  { 
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "error_message": "=join(' | ',@(1,&))"
      }
    }
  }
]

the modify transformation will work only for the case success = false without any adverse impact on the other case.

The demoes on the site enter image description here

for the case success = false :

enter image description here

  • Related