Search code examples
jsonapache-nifijolt

Convert string to Datetime with specific format using jolt transformation


I want to convert DateTime data to a specific DateTime format using jolt transformation in Nifi

Input:

[
  {
    "time": "02/15/2023 21:05:00",
    "actual_power": "0"
  },
  {
    "time": "02/15/2023 21:10:00",
    "actual_power": "0"
  }
]

Expected Output :

[
  {
    "time": "15/02/2023 21:05:00",
    "actual_power": "0"
  },
  {
    "time": "15/02/2023 21:10:00",
    "actual_power": "0"
  }
]

I am using the below JOLT. But it is not giving the expected output

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {      
      "time": {
        "toDate": ["$(0.time)", "MM/dd/yyyy HH:mm:ss"],
        "format": "dd/MM/yyyy HH:mm:ss"
      }
    }
  }
]

Please help me. I really appreciate any help you can provide.


Solution

  • You cannot directly manipulate through use of expression language for multiple different values of an attribute within a JoltTransformJSON specification , in this case it's time, but string manipulation within a modify transformation such as the below one is possible

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "mon_": "=substring(@(1,time),0,2)",
            "day_": "=substring(@(1,time),3,5)",
            "rest_": "=substring(@(1,time),6,19)",
            "time": "=concat(@(1,day_),'/',@(1,mon_),'/',@(1,rest_))"
          }
        }
      },
      {
        "operation": "remove",
        "spec": {
          "*": {
            "*_": ""
          }
        }
      }
    ]
    

    Otherwise; if time is a fixed repeated value within each object, then you can add an UpdateAttribute processor in which time is a variable with an example value "02/15/2023 21:05:00" entered in it and make the Jolt specification using expression language as

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "time": "${time:toDate('MM/dd/yyyy HH:mm:ss'):format('dd/MM/yyyy HH:mm:ss')}"
          }
        }
      }
    ]