12-18-2023 07:51 AM
Hello,
I need to filter a payload based on user entered parameters. The user can use the fieldsNames parameter to provide a list of fields they want returned, instead of all 30 attributes in the payload. In the examples below I need to filter the payload down to the list of fields specified in the fieldNames parameter. If an attribute in the gsn section is part of the fieldNames parameter then the gsn tag must be included else it can be left out. Any help is very much appreciated. Thank you.
Example1:
parameter: fieldNames = "rpn,gpn,artcode,gtin,grossWeight"
Example2:
parameter: fieldNames = "rpn,gpn,artcode"
Solved! Go to Solution.
12-19-2023 05:50 AM
@Max - with the solution provided, you can get the desired output you show above.
Only gsn:
Then without gsn:
By the way, if you are calling this from a triggered task to specify the fields you want, make sure you clear any default values from your task definition.
Hope this helps!
12-18-2023 08:38 AM
@Max - please download and decompress the attached zip file, then import the SLP file as a new pipeline. It is an example of how I recommend approaching this use case. Note that I opted to break out the gsn fields from the base-level products fields because handling those field lists in a single set requires object tree traversal which is not a simple task.
The pipeline as exported is showing your second use case where "gsn" fields are ignored. You can add "gsn" to the productFields pipeline parameter and "gtin,grossWeight" to the gsnFields pipeline parameter to see your first use case.
The JSON Generator is just your sample data.
The first Mapper (Remap gsn) is using the Mapping Root so I can work within the objects of that sub-element.
The expression is using the Object.filter() method to select only the fields listed in the "gsnFields" pipeline parameter. Within that callback function, I'm using the String.split() method to get an array containing the field names, then I'm using the Array.indexOf() method to see if the current field of the object is in that list.
The next Mapper (Remap Products) is doing very similar logic on the products array to filter down to the desired set of fields.
Hope this helps!
12-18-2023 09:56 AM
@koryknick- that solution worked great in filtering the base and GSN sections. Now i cant put the payload back together. How would i go about putting the output of each snap back into the proper format like below.
"products": [
{
"rpn": "009600-123",
"gpn": "G12345",
"artCode": "0019-10",
"gsn": [
{
"gtin": "10827123",
"grossWeight": ".005 gr"
}
]
}
]
12-18-2023 11:29 AM
@Max - looking at the output after the Remap Products snap, the document appears to be in the correct layout from what you show above.
Here are the pipeline parameters I'm using:
Hope this helps!
12-19-2023 05:39 AM - edited 12-19-2023 05:43 AM
@koryknickAfter playing with the solution for a while I have noticed a few issues. The way it is set up currently, the gsn section comes in one matter what, this is not correct. If i dont pass gsnFields i want only the baseFields exposed. Also, if the baseFields are not passed then I want only the gsnFields displayed. Examples below.
Example - no gsnFields
parameter productFields = rpn,gpn,artcode
"products": [
{
"rpn": "009600-123",
"gpn": "G12345",
"artCode": "0019-10"
}
]
Example - no baseFields
parameter gsnField = gtin,grossWeight