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

Array functions

rajendraj
New Contributor II

Is there a way to delete the first entry in an array if the length of the array is more than 1.

if the JSON looks like this:

{
โ€œcontentโ€: {
โ€œtypeโ€: โ€œSHOP_ITEMSโ€,
โ€œurisโ€: [
โ€œGroceriesโ€,
โ€œCleaning Productsโ€,
โ€œHousehold Itemsโ€
]
}
}

I want the final output to be

{
โ€œcontentโ€: {
โ€œtypeโ€: โ€œSHOP_ITEMSโ€,
โ€œurisโ€: [
โ€œCleaning Productsโ€,
โ€œHousehold Itemsโ€
]
}
}

7 REPLIES 7

dimitri_hristov
Contributor

Hi @rajendraj,

You could try and put this expression in a Mapper Snap:
($content.uris.length > 1) ? $content.uris.filter((item1, pos1, a1)=>(pos1 != 0)) : $content.uris
and assign it to the $content.uris Target Path.
Have the Pass Through option checked.

Hope this helps.
BR,
Dimitri

Yes, thanks for that. It solved my usecase. However, I bumped into next problem. Now that I have an array, while trying to insert this into postgresql where column is varchar, I get the following error:

PostgreSQL - Insert - Merge[5d2c0dcc9b085f032f5b6977_4f9e9ef4-21e4-4cc1-a7f8-3698051ca592 โ€“ bb2ca011-824e-40ed-ab8d-1377b1f37dde]
com.snaplogic.snap.api.SnapDataException: class java.util.ArrayList cannot be cast to class java.sql.Array (java.util.ArrayList is in module java.base of loader 'bootstrap'; java.sql.Array is in module java.sql of loader 'platform')
Do I have to explicitly cast it and if so what would it be?

Hi @rajendraj,

If youโ€™re having trouble writing the $content.uris array, posted in the original question, then you could try one of two things.

First, try and join the array elements in a string, like so $content.uris.join(โ€œseparator_of_your_choiceโ€). Then if youโ€™re using SnapLogic to read the data from the database, in order to transform the string data into an array, youโ€™d try stringData.split(โ€œseparator_of_your_choiceโ€).

Second, you could try and stringify the array, like so: JSON.stringify($content.uris). To transform the data from the DB, youโ€™d go with JSON.parse($stringifiedJSONArray).

BR,
Dimitri