Problem with nested mappings
Hi guys! I am quite new to SnapLogic and I am having a bit of a struggle with transformations. I worked with Mulesoft previously, and honestly, I am kinda trying to do things the same way but it doesn't really work here.
So this is my incoming JSON (it is quite ugly, I know):
{
"ID": "1234",
"Name": [
"Michael"
],
"parameters": [
[
[
{
"parameterOne": 1000,
"parameterTwo": 1200
}
],
[
{
"parameterOne": 20000,
"parameterTwo": 20000
}
],
[
{
"parameterOne": 10,
"parameterTwo": 1000
}
]
]
]
}
Main issue is the nested array (that Im not sure how to remove).
The result needs to look like this:
[{
"ID": "1234",
"Name": "Michael",
"parameterOne" : 21010
"parameterTwo" " 22200
}]
Name is always going to be 1 element in array, so it needs to be flattened.
As for parameterOne and parameterTwo - I need to add up all the values from the inner nested arrays and than also flatten them and put them into a result.
Any tip would be appreciated
Thank you!
Hi vonkendu,
You can use a wildcard operator to select all values from an array or an object, in your case from parameterOne and parameterTwo. After you get the needed array, using the reduce() function you can return the sum of all of the elements in the array.:jsonPath($, "$parameters[*][*][*].parameterTwo").reduce((accum, currentValue) => accum+ currentValue, 0)
jsonPath($, "$parameters[*][*][*].parameterTwo").reduce((accum, currentValue) => accum+ currentValue, 0)
If the name will always be an array with a single element, take the first element of the array to make it flattened.
Please check the attached pipeline.
Hope this helps!
BR.
Ivica