cancel
Showing results for 
Search instead for 
Did you mean: 

Splitting csv file into rows and then check if specified value has 10 elements

SL12345
New Contributor III

Hi snaplogic experts,

how can i split csv file into rows and then check if value after 8th semicolon has 10 values?

1;2566;28.3.2023;28.3.2023;10;10;15;11;1,2,3,4,5,6,7,8,9,10;Eur;Euro;USD
2;2567;28.3.2023;28.3.2023;10;10;15;11;1,2,3,4,5,6,7;Eur;Euro;USD
3;2566;28.3.2023;28.3.2023;10;10;15;11;1,2,3,4,5,6,7,8,9,10;Eur;Euro;USD

lets say i have values mentioned above, i can split file into rows ($content.split(‘\n’)) but how can i check in second step if after 8th semicolon there is 10 values? (values are separated with comma)

1st row will be true, 2nd row is false (because only 7 values) and 3rd will be true as well

thank you

1 ACCEPTED SOLUTION

viktor_n
Contributor II

Hi @SL12345,

If you want to do this with expression then try this:
$content.split("\n").map(row => {row: row, lengthValuse:row.split(';')[8].split(',').length })

image

Regards,
Viktor N.

View solution in original post

3 REPLIES 3

viktor_n
Contributor II

Hi @SL12345,

If you want to do this with expression then try this:
$content.split("\n").map(row => {row: row, lengthValuse:row.split(';')[8].split(',').length })

image

Regards,
Viktor N.

SL12345
New Contributor III

Thank you Viktor … and is there any way how to do it if i have input as a string ?

1;2566;28.3.2023;28.3.2023;10;10;15;11;1,2,3,4,5,6,7,8,9,10;Eur;Euro;USD\n2;2567;28.3.2023;28.3.2023;10;10;15;11;1,2,3,4,5,6,7;Eur;Euro;USD\n3;2566;28.3.2023;28.3.2023;10;10;15;11;1,2,3,4,5,6,7,8,9,10;Eur;Euro;USD\n

viktor_n
Contributor II

Hi @SL12345,

In the sample expression that I was writing, I was working on the sample with input as a string. Below is the sample that I was using.

[
    {
        "content": "1;2566;28.3.2023;28.3.2023;10;10;15;11;1,2,3,4,5,6,7,8,9,10;Eur;Euro;USD\n2;2567;28.3.2023;28.3.2023;10;10;15;11;1,2,3,4,5,6,7;Eur;Euro;USD\n3;2566;28.3.2023;28.3.2023;10;10;15;11;1,2,3,4,5,6,7,8,9,10;Eur;Euro;USD"
    }
]

For the expression to work, is expected the ‘content’ field on the input of the mapper with the CSV data inside as a string.

Regards,
Viktor N.