jsonjolt

JOLT spec to choose non null field aa result


I'm new to JOLT.

I have below Input :

{
  "F1": "1",
  "F2": "2"
}

I want the output like if F1 is null then it should return F2 value else it should return F1 value..

Expected Output :

{// if F1 is not null
  "result": "1"
}

Input:

{
  "F1": null,
  "F2": "2"
}

Expected Output :

{// as F1 is null
  "result": "2"
}

I tried with isNull but it's not working and not aware how to do


Solution

  • You rather can use notNull function along with Elvis Operator in which the function silently stops if F1 is null, and the argument 2 is returned by using following spec :

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "result": ["=notNull(@(1,F1))", "=(@(1,F2))"]
        }
      },
      {//pick up "result" attribute only
        "operation": "shift",
        "spec": {
          "result": "&"
        }
      }
    ]