Search code examples
jsonapache-nifijolt

Jolt replace text using regex


I am trying to transform an input data where I need to transform/replace the text on the basis of a regex. Not able to find any examples on how this can be done in Jolt spec. Could someone please help on this. Thanks!

Input

[
  {
    "name": "abc_google@gmail.com"
  },
  {
    "name": "ayan.agrawal$1990@gmail.com"
  }
]

Regex to be used

[^a-zA-Z0-9#$@]

Expected output:

[
  {
    "name": "abcgoogle@gmailcom"
  },
  {
    "name": "ayanagrawal$1990@gmailcom"
  }
]

Solution

  • You cannot do in the way of doing with a regex. Need to think on the contrary manner, eg. rendering individually by each character to be removed, and we can do it by successively applying split and join functions such as

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "nm0": "=split('\\.',@(1,name))",
            "nm1": "=join('',@(1,nm0))",
            "nm2": "=split('_',@(1,nm1))",
            "name": "=join('',@(1,nm2))"
          }
        }
      },
      {
        "operation": "remove",
        "spec": {
          "*": {
            "nm*": ""
          }
        }
      }
    ]