I have this JSON as input:
{
"my_array": []
}
If the array is empty I need to add a placeholder to the array, so this is the desired output:
{
"my_array": ["placeholder"]
}
If the array has a value/s:
{
"my_array": ["I have a value", "I am a second value"]
}
I need to keep those values on the array, so the desired output should be the equal to the input:
{
"my_array": ["I have a value", "I am a second value"]
}
So far I have tried using the default operation without success:
[
{
"operation": "default",
"spec": {
"my_array[]": [
"placeholder"
]
}
}
]
And I have also tried using a conditional with the shift operation without success:
[
{
"operation": "shift",
"spec": {
"my_array": {
"[]": {
"#placeholder": "my_array"
},
"*": {
"@(2,my_array)": "my_array"
}
}
}
}
]
This last code throws the error:
Error running the Transform.
JOLT Chainr encountered an exception constructing Transform className:com.bazaarvoice.jolt.Shiftr at index:0.
2
Answers
The first answer provided Barbaros Özhan works correctly. But if you have a jolt library verion that does not support the size function, this solution should work as well:
Edit: The second solution provided by Barbaros Özhan works as well
You can handle the issue through measuring the size of the array and put into the conditional whether it’s equal to zero or not such as
Edit : Considering that you have the older versions of the Jolt library, you can prefer using the following spec as an alternative