cancel
Showing results for 
Search instead for 
Did you mean: 

Filter attributes out of object

Max
New Contributor II

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"

"products": [
{
"gcpn": "82700123",
"rpn": "009600-123",
"productName": "someProduct",
"gpn": "G12345",
"productLineCode": "009XXX",
"artCode": "0019-10",
"gsn": [
{
"gtin": "10827123",
"parentGtin": "0082700123",
"grossWeight": ".005 gr",
"netWeight": ".005 gr",
"depth": ".005 cm",
"width": ".005 cm"
}
]
}
]
 
Final OutPut should be:
"products": [
{
"rpn": "009600-123",
"gpn": "G12345",
"artCode": "0019-10",
"gsn": [
{
"gtin": "10827123",
"grossWeight": ".005 gr"
}
]
}
]
 

Example2:

parameter: fieldNames = "rpn,gpn,artcode"

"products": [
{
"gcpn": "82700123",
"rpn": "009600-123",
"productName": "someProduct",
"gpn": "G12345",
"productLineCode": "009XXX",
"artCode": "0019-10",
"gsn": [
{
"gtin": "10827123",
"parentGtin": "0082700123",
"grossWeight": ".005 gr",
"netWeight": ".005 gr",
"depth": ".005 cm",
"width": ".005 cm"
}
]
}
]
 
Final OutPut should be:
"products": [
{
"rpn": "009600-123",
"gpn": "G12345",
"artCode": "0019-10"
}
]
1 ACCEPTED SOLUTION

koryknick
Employee
Employee

@Max - with the solution provided, you can get the desired output you show above.

Only gsn:

koryknick_0-1702993531737.png

koryknick_1-1702993565808.png

Then without gsn:

koryknick_3-1702993680652.png

koryknick_4-1702993712954.png

 

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!

View solution in original post

5 REPLIES 5

koryknick
Employee
Employee

@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.

koryknick_0-1702917334145.png

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. 

koryknick_3-1702917498144.png

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.

koryknick_2-1702917473693.png

Hope this helps!

Max
New Contributor II

@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"
}
]
}
]

koryknick
Employee
Employee

@Max - looking at the output after the Remap Products snap, the document appears to be in the correct layout from what you show above. 

koryknick_1-1702927752961.png

 

Here are the pipeline parameters I'm using:

koryknick_0-1702927689061.png

Hope this helps!

 

 

Max
New Contributor II

@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

"products": [
{
"gsn": [
{
"gtin": "00827002341494",
"grossweight": ".005gr"
}
]
}
]
 
Eaxmple 3 both parameters are passed
gsnFields = "gtin,grossWeight"
baseFields = "rpn,gpn,artCode"
 
"products": [
{
"rpn": "009600-123",
"gpn": "G12345",
"artCode": "0019-10",
"gsn": [
{
"gtin": "10827123",
"grossWeight": ".005 gr"
}
]
}
]