03-21-2024 02:58 AM
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!
Solved! Go to Solution.
03-21-2024 08:02 AM - edited 03-21-2024 08:03 AM
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
03-21-2024 08:02 AM - edited 03-21-2024 08:03 AM
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
03-22-2024 12:45 AM
Hello! Thank you very much, it works well!
One small follow up question, which should be a minor thing, but I just cannot figure this out.
Let's say I have a JSON input that looks like this:
[
[
{"value" : "Test"}
]
]
Again, there is a nested array, and I know the outer array is only gonna have a single value anyway, so I want to remove it to make it look just like this:
[
{"value" : "test"}
]
However, no matter what I do the outer array remains. I've tried doing it this way - just taking an array and writing it into the root
Also changing mapping root and rewriting into the root:
Or accessing something like this (this one for some reason takes only the first element of the inner array, which is very confusing to me also) :
There must be an easy way to flatten this array basically, right?
03-22-2024 03:08 AM
Via mapper .. if document array length will always be 1
or using JSON splitter snap if array length is greater than 1
03-26-2024 07:31 AM
Thank you very much!