โ04-21-2021 05:55 AM
Looking for a function to help add sequence numbers to a json array. End goal is to apply a sequence to matching values, starting with 01, subsequent values get a sequence of 02โฆ03โฆ 04 etc. I could do this in a mapper, or in apache velocity as part of a soap execute if possible to loop through, and apply therein.
What I have now:
[
{
โRecordsโ: [
{
โAwardโ: [
โAward_Aโ,
โAward_Cโ,
โAward_Aโ,
โAward_Bโ,
โAward_Cโ
]
}
]
}
]
Desired output:
[
{
โRecordsโ: [
{
โAwardโ: [
โAward_Aโ,
โAward_Cโ,
โAward_Aโ,
โAward_Bโ,
โAward_Cโ
],
โSequenceโ: [
โ01โ,
โ01โ,
โ02โ,
โ01โ,
โ02โ
]
}
]
}
]
Solved! Go to Solution.
โ04-21-2021 08:49 AM
@acesario , you can also just add [0]
after @viktor_nโs expression.
$Records.map(record => record.Award.map((x, index) => record.Award.slice(0, index + 1).filter(y => y == x).length))[0]
And if you really need the โ0โ you can try and add .map(val => "0" + val)
So full expression is: $Records.map(record => record.Award.map((x, index) => record.Award.slice(0, index + 1).filter(y => y == x).length).map(val => "0" + val))[0]
โ04-21-2021 07:14 AM
Hi @acesario,
Hope this will solve your problem.
Expression:
$Records.map(record => record.Award.map((x, index) => record.Award.slice(0, index + 1).filter(y => y == x).length))
Output:
โ04-21-2021 07:53 AM
@viktor_n Thanks for the response, great use of map approach! One thing I notice is that your resulting Sequence has a separate array for the sequence values [[1,1,2,1,2]] rather than
โSequenceโ: [โ01โ,โ01โ,โ02โ,โ01โ,โ02โ]
โ04-21-2021 08:35 AM
@acesario try this one.
$Records.map(record => {"Award": record.Award, "Sequence": record.Award.map((x, index) => record.Award.slice(0, index + 1).filter(y => y == x).length.toString())})
โ04-21-2021 08:49 AM
@acesario , you can also just add [0]
after @viktor_nโs expression.
$Records.map(record => record.Award.map((x, index) => record.Award.slice(0, index + 1).filter(y => y == x).length))[0]
And if you really need the โ0โ you can try and add .map(val => "0" + val)
So full expression is: $Records.map(record => record.Award.map((x, index) => record.Award.slice(0, index + 1).filter(y => y == x).length).map(val => "0" + val))[0]