Search code examples
jsonapache-nifijolt

Replace comma(,) with and in JOLT


Input:

{
  "ratings":["1","2"]
}

I need to replace this comma to and using JOLT. Is this possible in JOLT??

Expected Output:

{
  "ratings": "1 and 2"
}

Solution

  • Yes possible, you can use modify-overwrite-beta transformation along with string concatenation function join such as

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": "=join(' and ',@(1,&))"
        }
      }
    ]
    

    or prefer only

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "ratings": "=join(' and ',@(1,&))"
        }
      }
    ]
    

    for only one individual key-value pair(if there are more than one lists)

    Edit : In the case, you have an array with unsorted elements, and converting to the desired concatenated string after sorting whenever presumingly you use it within Apache-Nifi, then add two JoltTransformJSON processor, perform the below operation within the first processor ;

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "ratings": "=sort(@(1,&))"
        }
      }
    ]
    

    then apply one of the cases with the join function as the second step.