JOLT if else and moving the value of the root field to the nested

94 Views Asked by At

I'm new to Jolt transformations. I will try to describe my case in detail. I suspect that you will need to combine shift and modify-default operations

HERE: TRANSFORMATION RULE:
if field detail have value (exist and is not"" or null)
then map detail => updating_field
else:
map updating_field => updating_field

INPUT

{
  "updating_field": "ACCEPT",
  "channel": {
    "detail": null
  }
}

EXPECTED OUTPUT for e.g. detail = null value inside JOLT

{
  "channel": {
    "updating_field": "ACCEPT"
  }
}

OTHER INPUT

{
  "updating_field": "ACCEPT",
  "channel": {
    "detail": "normal_value"
  }
}

OTHER EXPECTED OUTPUT for e.g. **detail = "normal_value" value inside JOLT

{
  "channel": {
    "updating_field": "normal_value"
  }
}

Thanks in advance for any help.

1

There are 1 best solutions below

1
Barbaros Özhan On

You can use the ~ operator within a modify-overwrite-beta transformation such as

"~detail" : "@(2,updating_field)" meaning if "detail" does not exist or is null,

then make it be the value of "updating_field" after traversing 2 levels (: and {) up the tree such as

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "c*": {
        "~detail": "@(2,updating_field)"
      }
    }
  },
  { // rename the key from "detail" to "updating_field"
    "operation": "shift",
    "spec": {
      "c*": { // the level of "channel" where * stand for abbreviation
        "*": "&1.updating_field"
      }
    }
  }
]

the first demo on the http://jolt-demo.appspot.com/ site is :

enter image description here

the second demo on the http://jolt-demo.appspot.com/ site is :

enter image description here

Edit : If the null and empty string ("") values should be classifed as in the same category, then use ths size function as follows :

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "c*": {
        "sz": ["=size(@(1,detail))", 0]
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "c*": {
        "sz": {
          "0": { "@3,updating_field": "&3.updating_field" },
          "*": { "@2,detail": "&3.updating_field" } // else case
        }
      }
    }
  }
]