Search code examples
apache-nifijolt

NIFI Jolt JSON date transformation not working


In an Apache NIFI dataflow I try to transform a date from MM-dd-yyyy to yyyy-MM-dd (the default format which wil be accepted by MySQL as a date).

In the NIFI advanced editor of a JoltTransformJSON 1.15.0 Processor with DSL "Chain" I have entered:

Input Json:

{
  "Name": "Jan",
  "Birthday": "12-31-1994"
}

Transformation Jolt script:

[{
    "operation": "modify-overwrite-beta",
    "spec": {
        "Birthday": "=${Birthday:toDate('MM-dd-yyyy'):format('yyyy-MM-dd')}"
    }
}]

Result:

{
  "Name": "Jan",
  "Birthday": "12-31-1994"
}

I do not get any syntax errors. It seems the Jolt transformation does not change anything. Why is Birthday not transformed?

enter image description here


Solution

  • You can use the following consecutive specs within a JoltTransformJSON processor's specification part

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "b": "=split('-', @(1,Birthday))",
          "Birthday": "=concat(@(1,b[2]),'-',@(1,b[0]),'-',@(1,b[1]))"
        }
      },
      {
        "operation": "remove",
        "spec": {
          "b": ""
        }
      }
    ]
    

    where split the current date value by dashes and reorder them by use of concat function in the first step. Then remove the auxiliary b attribute in the last step

    enter image description here