ElielBerra
ElielBerra

Reputation: 96

Conditional With Arrays on a JOLT Transformation

I am having trouble handling an if conditional mixed with an array in a shift operation with jolt. If the field "HMEmails" is an array with values, no transformation should be made. But if it is an empty array or an empty string, the value should be transformed to many dots ("....."). I was able to detect when the field "HMEmails" is an empty string and insert many dots "....." as a value. And I was also able to leave the array as it is if it has values. However, I am not able to make the spec detect when the field "HMEmails" is an empty array, so that it can insert many dots "....." as a value.

This is the spec I built so far :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Place": "[&1].&",
        "HMEmails": {
          "*": {
            "@": "[&3].&2" // Leave the field as it is if it has values in the array
          },
          "": {
            "#.....": "[&3].&2" // Add ..... if HMEmails array comes as an empty string
          }
          // I am not able to make a spec match an empty array [] in this transformation
        },
        "Recruit": "[&1].&"
      }
    }
 }
]

For the input with "HMEmails" as an array with values, the outcome is as expected.

Input :

[
  {
    "Place": "Taiwan",
    "HMEmails": [
      "[email protected]",
      "[email protected]"
    ],
    "Recruit": "John"
  }
]

Output :

[
  {
    "Place": "Taiwan",
    "HMEmails": "",
    "Recruit": "John"
  }
]

For the input with "HMEmails" as an empty string, the outcome is as expected as well.

Input :

[
  {
    "Place": "Taiwan",
    "HMEmails": ".....",
    "Recruit": "John"
  }
]

Output:

[
  {
    "Place": "Taiwan",
    "HMEmails": ".....",
    "Recruit": "John"
  }
]

But for the input with "HMEmails" as an empty array, I do not found a way for the spec to match the expected outcome, which would me to have many dots "....." inserted as a value.

Income :

[
  {
    "Place": "Taiwan",
    "HMEmails": [],
    "Recruit": "John"
  }
]

Outcome :

[
  {
    "Place": "Taiwan",
    "Recruit": "John"
      // Note that the field "HMEmails" is gone
  }
]

Expected outcome :

[
  {
    "Place": "Taiwan",
    "HMEmails": ".....", // This is the expected outcome
    "Recruit": "John"
  }
]

Any idea on how to proceed? thanks a lot! .

Upvotes: 1

Views: 510

Answers (2)

Mohammadreza Khedri
Mohammadreza Khedri

Reputation: 2711

You can change your HMEmails field to a string that started with data-. So you can write your own conditions like the below example in the second shift.

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "HMEmails": "=concat('data-',@(1,HMEmails))",
        "empty": ""
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Place": "[&1].&",
        "HMEmails": {
          "data-\\[*\\]": { // not empty array
            "@(2,empty)": "[&3].&2"
          },
          "data-\\[\\]|data-": { // empty array or empty string
            "#.....": "[&3].&2"
          }
        },
        "Recruit": "[&1].&"
      }
    }
  }
]

Upvotes: 1

Barbaros Özhan
Barbaros Özhan

Reputation: 65408

Using a modify transformation spec with toString function as below would be sufficient to use

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "HMEmails": ["=toString", "....."]
      }
    }
  }
]

which brings the second argument "....." whenever the function doesn't work for the values, such as [] or null , of the first argument

Upvotes: 1

Related Questions