Search code examples
jsonnullconcatenationjolt

JOLT concatenate ONLY if not NULL


I need to change a date from YEAR to a fixed YYYY-MM-DD only if the value of the year is not null.

At the moment I have this

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "Passing?": "=concat(@(1,Passing),-01-01'"
    }
  }
]

Input:

{
  "Passing": "2022",
  "Comments": "NotMentioned",
  "Team": "2",
  "modified_date": "2023-01-27 13:41:39",
  "Profile": "DM",
  "JobRole": "Tester"
}

And the Output I'll like is

{
  "Passing": "2022-01-01",
  "Comments": "NotMentioned",
  "Team": "2",
  "modified_date": "2023-01-27 13:41:39",
  "Profile": "DM",
  "JobRole": "Tester"
}

This is currently working fine but when I have a null value in Passing i get this, that I want to avoid.

{
  "Passing": "null-01-01",
  "Comments": "NotMentioned",
  "Team": "2",
  "modified_date": "2023-01-27 13:41:39",
  "Profile": "DM",
  "JobRole": "Tester"
}

I need to keep the "Passing" equal to "null" if there is no year in it.


Solution

  • The suffixing ? operator of Passing attribute checks out for the existence, not for nullability.

    You rather can use isNull function within a modify transformation spec in order to check the condition such as

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "Passing": ["=isNull", "=concat(@(1,&),'-01-01')"]// the value is overwritten provided the Passing has a non-null value
        }
      }
    ]
    

    where @(1,&) replicates the current value of the attribute Passing