bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.56k stars 330 forks source link

Apply if else condition in Jolt #1084

Open yulyu123 opened 3 years ago

yulyu123 commented 3 years ago

Hi,

I need help on the following transformation.

Input:

{
  "job": {
    "terminationDate": "null"
  },
  "accessRevocation": {
    "accessRemovalDate": "2021-01-03"
  }
}

Expected output:

{
  "job" : {
    "terminationDate" : "null"
  },
  "accessRevocation" : {
    "accessRemovalDate" : "null"
  }
}

So the logic is basically checking whether terminationDate is null or not. If it is null, then accessRevocation.accessRemovalDate needs to be modified to null, otherwise, keep it as is.

Something I have tried out:

[
  {
    "operation": "shift",
    "spec": {
      "job": {
        "@": "job",
        "terminationDate": {
          "|null": {
            "#null": "&3.accessRevocation.accessRemovalDate"
          }
        }
      },
      "*": "&"
    }
  }
]

But the output is

{
  "job" : {
    "terminationDate" : "null",
    "accessRevocation" : {
      "accessRemovalDate" : "null"
    }
  },
  "accessRevocation" : {
    "accessRemovalDate" : "2021-01-03"
  }
}

Seems like instead of modifying the existing accessRemovalDate, my jolt spec is simply adding a new object under job blob.

Could you please help with this? Thanks a million!

loveall commented 3 years ago

May not be best solution , but try below one

[
  {
    "operation": "shift",
    "spec": {
      "job": {
        "@": "job",
        "terminationDate": {
          "|null": {
            "$": "accessRevocationTemp[].accessRemovalDate"
          }
        }
      },
      "*": "accessRevocationTemp[]"
    }
  }, {
    "operation": "shift",
    "spec": {
      "job": "job",
      "accessRevocationTemp": {
        "0": {
          "accessRemovalDate": "accessRevocation.accessRemovalDate"
        }
      }
    }
  }
]
lucioalmeida commented 3 years ago
[
  {
    "operation": "shift",
    "spec": {
      "job": "job",
      "accessRevocation": {
        "accessRemovalDate": {
          "@(3,job)": {
            "terminationDate": {
              "|null": null,
              "*": {
                "@3": "accessRevocation.accessRemovalDate"
              }
            }
          }
        }
      }
    }
  }, {
    "operation": "default",
    "spec": {
      "accessRevocation": {
        "accessRemovalDate": null
      }
    }
  }
]