'Jolt Transform: Single Value can't be added to an Array
I have the following Jolt Spec
[
{
"operation": "shift",
"spec": {
"*": {
"value": "[]"
}
}
},
{
"operation": "shift",
"spec": {
"*": "values"
}
},
{
"operation": "default",
"spec": {
"values": []
}
}
]
For the following Input:
[
{
"value": 175
},
{
"value": 160
}
]
I get the expected result as following:
{
"values" : [ 175, 160 ]
}
And for the following Input:
[
{
"valueNum": 175
}
]
I again get an expected result as follows:
{
"values" : [ ]
}
But for the following input :
[
{
"value": 175
}
]
I get the following output
{
"values" : 175
}
I want to have the values in an array even if there is just one element in it like below:
{
"values" : [175]
}
Could you please help me fixing my Jolt Spec to get the desired result? Thanks!
Solution 1:[1]
Just using a single shift transformation spec as the following one would suffice
[
{
"operation": "shift",
"spec": {
"*": {
"*": "&s[]"
}
}
}
]
where &
is a substitution for the key name(value
), and s
is a suffix to make the word plural
the demoes on the site http://jolt-demo.appspot.com/ are
Edit : If you need an output based on seperating the keys of the attributes to be value
or the others(such as valueNum
), then still a single spec like the one below would be sufficient :
[
{
"operation": "shift",
"spec": {
"*": {
"value": "&s[]",
"*": "&[]"
}
}
}
]
Solution 2:[2]
With the following spec, I was able to achieve all my use cases
[
{
"operation": "shift",
"spec": {
"*": {
"value": "[]"
}
}
},
{
"operation": "shift",
"spec": {
"*": "values[]"
}
},
{
"operation": "default",
"spec": {
"values": []
}
}
]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Barbaros Özhan |