Search code examples
jsontransformjolt

condition for case-insensitive contains jolt to transform


Input

{
  "type": "my Mnl"
}

type can contains values like "My mnlString", "My Manual String", "Credit mnl String"

Irrespective of case I want to transform json where type to MANUAL_CREDIT when value contains ("mnl" or "Manual") & "Credit" or (credit) (case-insensitive)

Irrespective of case I want to transform json where type to MANUAL_DEBIT when value contains ("mnl" or "Manual") & "Debit" or "Debit" (case-insensitive)

expected output

{
 "type" : "MANUAL_DEBIT"
} 

or

{
 "type" : "MANUAL_CREDIT"
} 

Default

{
 "type" : "MANUAL_PAYMENT"
} 

Modified Specs for if else-if else

[
  { // convert all letters of the expression to lowercase
    "operation": "modify-overwrite-beta",
    "spec": {
      "type": "=toLower"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "type": {
        "*mnl|*mnl*|mnl*|*manual|*manual*|manual*|*credit|*credit*|credit*": {
          // pipes provide OR logic
          // be careful that the search string might be 
          // in the middle, start or end
          "#MANUAL_PAYMENT": "&2" // &2 copies the literal "type"
        },
        "*credit|*credit*|credit*": {
          "#CREDIT_PAYMENT": "&2"
        },
        "*": {
          "#CARD_PAYMENT": "&2" // # wildcard generates fixed expression
        }
      }
    }
  }
]

Getting following error

Error running the Transform.
JOLT Chainr encountered an exception constructing Transform className:com.bazaarvoice.jolt.Shiftr at index:4.

Solution

  • You can use a conditional within a shift transformation spec after converting all cases of the letters to lower or upper in order to use in literal comparison easily such as

    [
      { // convert all letters of the expression to lowercase
        "operation": "modify-overwrite-beta",
        "spec": {
          "type": "=toLower"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "type": {
            "*mnl|*mnl*|mnl*|*manual|*manual*|manual*": {
              // pipes provide OR logic
              // be careful that the search string might be 
              // in the middle, start or end
              "#MANUAL_PAYMENT": "&2" // &2 copies the literal "type"
            },
            "*": {
              "#CARD_PAYMENT": "&2" // # wildcard generates fixed expression
            }
          }
        }
      }
    ]
    

    Edit: You might use the following shift transformation spec based on the last modification :

      {
        "operation": "shift",
        "spec": {
          "type": {
            "*mnl|*mnl*|mnl*|*manual|*manual*|manual*|*manual*credit*|*manual*credit|manual*credit*": {
              "#MANUAL_PAYMENT": "&2"
            },
            "*credit|*credit*|credit*": {
              "#CREDIT_PAYMENT": "&2"
            }
          }
        }
      }