Convert CSV file to JSON format

Dear community,

I’m working on a project where i should retrieve a csv file from the device, convert it to JSON format in order to be able to store it on the mobile internal table.
I used the pick file flow function to select the csv file, the read flow function to get the utf8 file string and i want to use i JS function to convert the UTF8 to JSON format and store the result on the mobile device.

The JS code i used is the following :
function csvJSON(input1) {
const lines = input1.split(/\r\n|\n/);
const result = ;
const headers = lines[0].split(/,/);
for (let i = 1; i < lines.length; i++) {

    const obj = {};
    const currentline = lines[i].split(/,/);
    for (let j = 0; j < headers.length; j++) {
        obj[headers[j]] = currentline[j];
    };
    result.push(obj);
};
return JSON.stringify(result);

};

The result variable i defined is a list of objects with 2 properties.

This JS function is not working, could you please help me with that ?

Thank you for your help.

Best regards,
Marouane

Most people here know little to no code at all, like me, hence cannot help.
A few experts may be able to help but you’ll have to be patient. There are also a few unwelcomed web dev agencies here that could assist but sadly won’t help, all they want is your money.
So I recommend you to post your question on Stackoverflow where you may get an answer faster than here.

Thank you for the advice Fred, i will try posting on Stackoverflow too.

Best regards,
Marouane

@Marouane_EL_BAHIR
But by the way, you may not really have to use custom javascript for such a purpose…

You could do a combination of MAP(), SPLIT() formulas to achieve this without the javascript.
I am not 100% sure what your goal is from this script, but example:
If a csv is formated as this:

header1,header2,header3/
cell1,cell2,cell3/
cell4,cell5,cell6

Formula that could theoretically work:

MAP<row>(SPLIT(data.myCsv, "/"), BUILD_OBJECT(MAP<cells>(SPLIT(row, ","), {key: SPLIT(SPLIT(data.myCsv, "6/")[0], ",")[index], value: cells}), "key", "value"))

*you can copy the above single line, to paste it into the formula editor and below I try to explain

MAP<row>(
   SPLIT(data.myCsv, "/"), 
      BUILD_OBJECT(
         MAP<cells>(
            SPLIT(row, ","), 
               {key: SPLIT(SPLIT(data.myCsv, "6/")[0], ",")[index], value: cells}
            ), 
         "key", "value"
      )
)

I am not convinced that this will work as I didn’t test it, but in theory what this whole thing does?:

We get the csv file and split it into rows with the SPLIT(data.myCsv, “/”) formula. The outermost MAP() then will go through each of these rows (hence we use the alias “row”)
And for each row it will “Build an object” from the values of the current row’s cells, and the header row’s cells as the keys. That’s what we want, right?
This is done by another MAP() which is the equivalent of your internal FOR loop. But of course to feed the information of the header row we have to access it via its static [0] index in the list of the rows. Then we also need to split that into separate cells, this is done by the 2 SPLIT(SPLIT()) formulas. We find the correct header for our cell’s value by feeding it the same index as the index of the cell. And we also get the value of the cell as referred to in the “cells” alias.
In the end we tell the “BUILD_OBJECT” formula that it should look for the keys in the “key” property of the list of objects above and the values in the “value” property.

Let me know if this solves your issue with a no-code approach, or if you face any other problems.

*#AGisCool, I mean this is a truly powerful tool if you master the formulas as well as the style and the positions tab