bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.54k stars 328 forks source link

Add indexOf and lastIndexOf functions for string #1153

Open sibmaks opened 2 years ago

sibmaks commented 2 years ago

Use case for these functions:

If you need to split a string into two parts by any character or substring, it is useful to have these functions.

Masking email exmaple

Transform json from:

{
    "email": "user@domain.com"
}

To

{
    "email": "u***@domain.com"
}

Current solution

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "emailLength": "=size(@(1,email))",
      "emailParts": "=split('@', @(1,email))",
      "userNameLength": "=size(@(1,emailParts[0]))",
      "userNameSub": "=substring(@(1,email), 0, 1)",
      "domainPart": "=substring(@(1,email), @(1,userNameLength), @(1,emailLength))",
      "email": "=concat(@(1,userNameSub), '***', @(1,domainPart))"
    }
  },
  {
    "operation": "remove",
    "spec": {
      "emailLength": "",
      "emailParts": "",
      "userNameLength": "",
      "userNameSub": "",
      "domainPart": ""
    }
  }
]

With indexOf

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
        "emailLength": "=size(@(1,email))",
        "dividerIndex": "=indexOf(@(1,email), '@')",
        "userNameSub": "=substring(@(1,email), 0, 1)",
        "domainPart": "=substring(@(1,email), @(1,dividerIndex), @(1,emailLength))",
        "email": "=concat(@(1,userNameSub), '***', @(1,domainPart))"
    }
  },
  {
    "operation": "remove",
    "spec": {
      "emailLength": "",
      "dividerIndex": "",
      "userNameSub": "",
      "domainPart": ""
    }
  }
]