Search code examples
javajsonjolt

How do I perform padding operations on an inconsistent field using JOLT left/rightpad


Let's say I have the following input:

{
  "input": {
    "string": "the QuIcK brOwn fox"
  }
}

I am trying to pad the 'string' field to a specific length. Utilising the following would not be possible:

"basic1": "=leftPad('the QuIcK brOwn fox', 30, 'X')"

The value of the 'string' field will be dynamic and subject to change each time. Hence I require a transformation that can be applied to the 'string' key as opposed to simply manipulating the value.

I have tried the following in the jolt transform demo website:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "rightPad": {
        "basic4": "=rightPad(@(2,string), 30, 'X')" // happy path : actually pad a looked up string from the data
      }
    }
  }
]

I am receving the following output on the demo website for JOLT:

{
  "input" : {
    "string" : "the QuIcK brOwn fox"
  },
  "rightPad" : { }
}

Solution

  • You can use the following modify transformation spec

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": { // overwrites the value of the "string" attribute
            "string": "=rightPad(@(1,&), 30, 'X')" 
            // @(1,&) looks for the current level 
            // with replicated value of "string" due to the ampersand
          }
        }
      }
    ]