jsontransformationjolt

Is there a way to add a prefix to all elements in array in jolt?


I would like to add a constant prefix eg."0" to all the fields in an array.

This is the input:

{
  "input": [
    "1_ANY",
    "2_ANY",
    "3_ANY"
  ]
}

Output:

{
  "output" : [ "1", "2", "3" ]
}

Expected Output:

{
  "output" : [ "01", "02", "03" ]
}

I have tried with below jolt transformation Spec :

[
  {
    "operation": "shift",
    "spec": {
      "input": {
        "*": {
          "*_*": {
            "$(0,1)": "output"
          }
        }
      }
    }
  }

]

Is there a way I can add a constant prefix to all elements in the array of strings ?


Solution

  • You can add modify-overwrite-beta spec to the one you created to do the concatenation as follows:

    [
      {
        "operation": "shift",
        "spec": {
          "input": {
            "*": {
              "*_*": {
                "$(0,1)": "output"
              }
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "output": {
            "*": "=concat('0',@0)"
          }
        }
      }
    
    ]
    

    Or you can do it using two shift operations as follows:

    [
      {
        "operation": "shift",
        "spec": {
          "input": {
            "*": {
              "*_*": {
                "$": "0&(1,1)"
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "$": "output"
          }
        }
      }
    
    ]