I'm rather new to JOLT transformations so I'm still having trouble with pretty basic stuff.
I have the following spec:
{
"operation": "shift",
"spec": {
"positions": "positions",
"line_items": {
"*": {
"@(sku)": "id&",
"@(quantity)": "quantity&"
}
}
}
}
That generates this output out of some test data:
{
"positions" : 2,
"id0" : "149",
"quantity0" : 1,
"id1" : "4270",
"quantity1" : 1,
"id2" : "1440",
"quantity2" : 3,
}
My problem is that I need the attribute index to start at 1 instead of 0 ("id0" and "quantity0" should be "id1" and "quantity1" and so on)
I've been stuck trying to figure this one out :(
Any suggestions?
I tried googling this, play around with the spec myself, and even chatGPT... still can't seem to be any closer to an answer.
Considering
{
"positions": 2,
"line_items": [
{
"sku": "149",
"quantity": 1
},
{
"sku": "4270",
"quantity": 1
},
{
"sku": "1440",
"quantity": 3
}
]
}
is your input based on the current spec and result, you can add an extra object by using default transformation spec which will generate an extra object to be the first one after sorted such as
[
{
"operation": "default",
"spec": {
"line_ite": [{
"sku": "000000",
"quantity": "000000"
}]
}
},
{ // to prepare the latest object the be the first one within the new array
"operation": "sort"
},
{
"operation": "shift",
"spec": {
"positions": "&",
"line_it*": {
"*": {
"sku": "id",
"*": "&"
}
}
}
},
{
"operation": "shift",
"spec": {
"positions": "&",
"id|qua*": {
"0": { "": "" }, // get rid of the lately generated object
"*": {
"@": "&2&" // replicate the value with prefixed key which is reformed by going 2 levels up the tree
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is