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! .
2
Answers
You can change your
HMEmails
field to a string that started withdata-
. So you can write your own conditions like the below example in the secondshift
.Using a modify transformation spec with toString function as below would be sufficient to use
which brings the second argument
"....."
whenever the function doesn’t work for the values, such as[]
ornull
, of the first argument