cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Problem with nested mappings

vonkendu
New Contributor II

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!

1 ACCEPTED SOLUTION

ivicakoteski
New Contributor III

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.

ivicakoteski_0-1711032266115.png

Please check the attached pipeline.
Hope this helps!

BR.
Ivica

View solution in original post

4 REPLIES 4

ivicakoteski
New Contributor III

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.

ivicakoteski_0-1711032266115.png

Please check the attached pipeline.
Hope this helps!

BR.
Ivica

vonkendu
New Contributor II

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

vonkendu_0-1711093327291.png

Also changing mapping root and rewriting into the root:

vonkendu_1-1711093400942.png

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) :

vonkendu_2-1711093479784.png

There must be an easy way to flatten this array basically, right? 

 

 

 

 

alchemiz
Contributor III

Via mapper ..  if document array length will always be 1

alchemiz_0-1711102006265.png

 

or using JSON splitter snap if array length is greater than 1

alchemiz_1-1711102092257.png

 



vonkendu
New Contributor II

Thank you very much!