Search code examples
javascriptcsvpapaparse

Javascript how to parse csv to create new columns


I have a .csv with description strings in its rows, I need to parse their content but if there's a \n or , in it the parsing goes wrong.

With papa parse I can get headers and rows values in arrays, but then I need to parse the description value (rowsId[4]) and split it into columns.

.csv (exported from ecommerce):

    ID,COD,Name,Dscription,Meta
    1234,#324,box"<ul>
    \n<li>Dimensions: 37 x 45,5 x 203,5(h) cm</li>
    \n<li>made of wood</li>
    \n</ul>",789

Expected output:

rowId = [1234, #324, "box", "Dimensions: 37 x 45,5 x 203,5(h) cm", "made of wood", 789];

.csv (final)

    ID,COD,Name,Dscription1,Dscription2,Meta
    1234,#324,box,"Dimensions: 37 x 45,5 x 203,5(h) cm","made of wood",789

How can I split excluding the \n and , found between " " ?


Solution

  • You can use a regular expression for that:

    let rawData = "<ul>\
        \n<li>Dimensions: 37 x 45,5 x 203,5(h) cm</li>\
        \n<li>made of wood</li>\
        \n</ul>"
     
    console.log(rawData)
    
    const regexp = /<li>(.*)<\/li>/g
    const data = rawData.match(regexp).map(el => {
        return el.replace(/<\/?li>/g, '')
    })
    
    console.log(data)
    // Array [ "Dimensions: 37 x 45,5 x 203,5(h) cm", "made of wood" ]