Reputation: 9
I have an array object amount and a variable amountVar. I need to insert the value in amountVar into amount array at 2nd position.
Here is the input JSON Input
{
"PTL" : {
"TA" : [ {
"T" : [ {
"amount" : [ 10, 11, 21 ],
"amountVar" : 7
} ]
}, {
"T" : [ {
"amount" : [ 20, 22, 22 ],
"amountVar" : 6
} ]
}, {
"T" : [ {
"amount" : [ 30, 33, 23 ],
"amountVar" : 16
} ]
} ]
}
}
Tried using modify-overwrite-beta operation to rewrite the array by specifying the elements to insert { "operation": "modify-overwrite-beta", "spec": { "PTL": { "TA": { "": { "T": { "": { "amount": [ "@(1,amount[0])", "@(1,amountVar)", "@(1,amount[1])", "@(1,amount[2])" ] } } } } } } }
Expected output
{
"PTL" : {
"TA" : [ {
"T" : [ {
"amount" : [ 10, 7, 11, 21 ]
} ]
}, {
"T" : [ {
"amount" : [ 20, 6, 22, 25 ]
} ]
}, {
"T" : [ {
"amount" : [ 30, 16, 33, 23 ]
} ]
} ]
}
}
Here is the input JSON Input
{
"PTL" : {
"TA" : [ {
"T" : [ {
"amount" : [ 10, 11, 21 ],
"amountVar" : 7
} ]
}, {
"T" : [ {
"amount" : [ 20, 22, 22 ],
"amountVar" : 6
} ]
}, {
"T" : [ {
"amount" : [ 30, 33, 23 ],
"amountVar" : 16
} ]
} ]
}
}
Tried using modify-overwrite-beta operation to rewrite the array by specifying the elements to insert { "operation": "modify-overwrite-beta", "spec": { "PTL": { "TA": { "": { "T": { "": { "amount": [ "@(1,amount[0])", "@(1,amountVar)", "@(1,amount[1])", "@(1,amount[2])" ] } } } } } } }
Expected output
{
"PTL" : {
"TA" : [ {
"T" : [ {
"amount" : [ 10, 7, 11, 21 ]
} ]
}, {
"T" : [ {
"amount" : [ 20, 6, 22, 25 ]
} ]
}, {
"T" : [ {
"amount" : [ 30, 16, 33, 23 ]
} ]
} ]
}
}
Upvotes: 0
Views: 74
Reputation: 106
one way to solve this problem is to use the =toList()
function.
First you have to split the already existing list and converting it into a dictionary:
so for example the first "amount" would look like this:
"amount" : {
"0" : 10,
"1" : 11,
"2" : 21
}
And then putting the corresponding "amountvar" in the same amount.
After that you can use the =toList()
function to create a list with the wanted elements in any order you desire.
Finally you extract the list you created and remove the other variables.
Here is the spec:
[
{
"operation": "shift",
"spec": {
"PTL": {
"TA": {
"*": {
"T": {
"*": {
"amount": {
"*": {
"@0": "&7.&6[&5].&4[&3].&2.&0"
},
"@(1,amountVar)": "&6.&5[&4].&3[&2].&1.var"
}
}
}
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"PTL": {
"TA": {
"*": {
"T": {
"*": {
"amount": {
"list": "=toList(@(1,0),@(1,var),@(1,1),@(1,2))"
}
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"PTL": {
"TA": {
"*": {
"T": {
"*": {
"amount": {
"@(0,list)": "&6.&5[&4].&3[&2].&1"
}
}
}
}
}
}
}
}
]
This only works for lists with 3 elements in it, if there were more, you had to manually change the toList function, adding every element in the order you prefer
Upvotes: 0