โ01-16-2021 04:31 AM
Hello, I have an issue with splitting a JSON document in a mapper.
In short, this document is received via a REST Get Snap and certain elements of it should be mapped to certain names, think for example:
Received JSON:
{
โProperty1โ:โvalue1โ,
โProperty2โ:โvalue2โ,
โProperty3โ:โvalue3โ,
โSubarrayโ:[
{
โSubProperty1โ:โsubValue1โ,
โSubProperty2โ:โSubValue2โ,
โSubProperty3โ:โSubValue3โ
},
{
โSubProperty1โ:โsubValue4โ,
โSubProperty2โ:โSubValue5โ,
โSubProperty3โ:โSubValue6โ
}
]
}
And the produced JSON should look something like this:
{
โProp1โ:โvalue1โ,
โProp2โ:โvalue2โ,
โMyArrayโ:[
{
โSubProp1โ:โsubValue1โ,
โSubProp2โ:โSubValue2โ,
},
{
โSubProp1โ:โsubValue4โ,
โSubProp2โ:โSubValue5โ,
}
]
}
In and of itself, this should be a simple task, we have only to map some input values to some output values. However when mapping the values in the subarray like so:
$ SubProperty1 โ $Prop1
$ SubProperty2 ->$Prop2
jsonPath($, "$SubArray[].SubProperty1") โ $MyArray[].SubProp1
instead of producing the second JSON as required it produces something like this:
{
โProp1โ:โvalue1โ,
โProp2โ:โvalue2โ,
โMyArrayโ:[
{
โSubProp1โ:[
โsubValue1โ,
โsubValue4โ
],
โSubProp2โ:[
โSubValue2โ,
โSubValue5โ
]
}
]
}
So it seems that instead of splitting the values of the elements in SubArray between the elements in MyArray, it sees the array of the elements as one unit and therefore populates only the first element in MyArray with a vector containing the elements of SubArray.
Iโve been trying various things but I canโt seem to solve this, any help would be appreciated.
Note that since the data comes from a Rest Call, snaplogic doesnโt know until execution what the data should look like.
I hope that was clear, the problem itself is rather simple but the explanation is somehow a little complicated.
Thanks in advance!
Solved! Go to Solution.
โ01-20-2021 08:30 AM
you can use the match operator to match the each doc in the array and map it to whatever youโd like
so using this expression:
$Subarray.map(entry => match entry { {SubProperty1, SubProperty2} => {"SubProp1": SubProperty1, "SubProp2": SubProperty2}, _ => []})
mapped to MyArray
should give you the desired output for the nested array portion. The rest seems to just be direct mappings from source Property1
to target Prop1
, Property2
to target Prop2
while setting pass through to off.
One thing to note, the โ_โ is used as a default incase there is a document that does not match any of the arms in the match. So, if there is a doc that does not have these fields it would default to an empty array, which you can change.
โ01-17-2021 07:29 PM
Try two Mappers, one to map the fields of the root object, then another with Mapping Root set to the array. That will let you map the elements of that array, and pass through the root object from the first Mappers.
โ01-18-2021 12:07 AM
Iโve tried that but it causes the same issue as with one mapper.
The elements of the subarray still get combined instead of being mapped to separate arrays.
The issue is really that the subarray is an array of arrays, which Iโd like to reproduce. Instead the mapper produces elements, each of wich contains an array of all of one type of subvalue from the different subarrays in the input.
Sorry I just noticed that the formating in the JSON didnโt survive copying which doesnโt help make that obvious.
โ01-18-2021 08:57 AM
Yes, Iโm not seeing any nested arrays in the JSON input or output you posted, except for the one where youโre saying that this isnโt what you want. Try posting the input and desired output with the Preformatted Text feature in the editor (icon looks like </>). That will also prevent the double quotes from getting inserted as left/right versions like in English text, which is invalid JSON.
[
{"name": "value"},
{"name", "value"}
]
โ01-19-2021 09:54 AM
Did you still need help with this?