How can I iterate through list and replace some elements in it?
I have this input
{
"clients": [
{
"clientId": "166734",
"info": {
"cards": [
"378282246310005",
"371449635398431"
]
}
}
]
}
What I expect cards will looks like this "cards" : [ "3782", "3714" ]
But in my spec do not work substring
[
{
"operation": "modify-overwrite-beta",
"spec": {
"clients": {
"*": {
"info": {
"cards": {
"*": "=substring(@(1,&),0,@(1,4))"
}
}
}
}
}
}
]
2
Answers
You can use shift transformations to tame the cards array in order to prepare for modify transformation spec such as
If there can always only exist two components for cards then we make the whole spec shorter by using [first/last]Element functions, otherwise use
"=(@(1,cards[0/1]))"
by individually writing such asNote 1:
You should not use
@(1,&)
for getting an array index.@(1,&)
says: Go up 1 level (cards
) and get0
and1
key from the array with&
. But You have an array and should get the index from it.You can say get
&
in an array like this:@(1,[&])
Note 2:
For getting the first 4 elements in the string you don’t need the
@(1,4)
. just say4
Simpler solution:
We can get the current level without going up 1 level with
@(0)
or@0
:@0,0,4