prathik vijaykumar
prathik vijaykumar

Reputation: 385

truncate a part of string using jolt transform

Input json :

{
  "userid" : "31b25f023c58c969388991a6b9b4030000000000",
  "username_id": "54fca0dd914ae593ef65988b4a3e93cccc590000000000"
}

There is 10 0's ahead of each value. The overall length of string can vary, but it will be appended by 10 0's always. I would like to remove it.

Thus expected output:

{
  "userid" : "31b25f023c58c969388991a6b9b403",
  "username_id": "54fca0dd914ae593ef65988b4a3e93cccc59"
} 

Hence i am looking for the right way using jolt transform to perform this truncation.

Upvotes: 0

Views: 1608

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65323

You can start with a modify transformation in which the substring function is applied for each value of the attributes after determining their ten reduced length in order to extract the value except for the last ten characters, as it's mentioned ten zeroes are always right-padded, such as

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "userid_len": "=size(@(1,userid))",
      "userid_len_dif": "=intSum(-10,@(1,userid_len))",
      "userid": "=substring(@(1,&),0,@(1,userid_len_dif))",
      "username_id_len": "=size(@(1,username_id))",
      "username_id_len_dif": "=intSum(-10,@(1,username_id_len))",
      "username_id": "=substring(@(1,&),0,@(1,username_id_len_dif))"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "use*id": "&"
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is

enter image description here

Upvotes: 2

Related Questions