Search code examples
javascriptcsvparsingdouble-quotesdeclared-property

how to declare a variable with csv content with semicolon and double quotes in javascript


You are provided with the following CSV file content as a variable:

"Buchungstag";"Wertstellung (Valuta)";"Vorgang";"Buchungstext";"Umsatz in EUR";
"22.10.2020";"22.10.2020";"Übertrag / Überweisung";"Auftraggeber: XY Buchungstext: KD 1 RE 3000 Ref. 123/456";"18,80";
"19.10.2020";"19.10.2020";"Übertrag / Überweisung";"Auftraggeber: AB Buchungstext: KD 1 RE 3000 Ref. 123/457";"160,00";

The goal is to parse this CSV file content, but when I declare the variable, it takes only the first value because the semicolon is the divider and it gives me a syntax error for all the other content of the CSV.

var csv= "Buchungstag";"Wertstellung (Valuta)";"Vorgang";"Buchungstext";"Umsatz in EUR"; "22.10.2020";"22.10.2020";"Übertrag / Überweisung";"Auftraggeber: XY Buchungstext: KD 1 RE 3000 Ref. 123/456";"18,80"; "19.10.2020";"19.10.2020";"Übertrag / Überweisung";"Auftraggeber: AB Buchungstext: KD 1 RE 3000 Ref. 123/457";"160,00";

error


Solution

  • You can put the entire value between single quotes:

    const csv = '"Buchungstag";"Wertstellung (Valuta)";"Vorgang";"Buchungstext";"Umsatz in EUR";\n
    "22.10.2020";"22.10.2020";"Übertrag / Überweisung";"Auftraggeber: XY Buchungstext: KD 1 RE 3000 Ref. 123/456";"18,80";\n
    "19.10.2020";"19.10.2020";"Übertrag / Überweisung";"Auftraggeber: AB Buchungstext: KD 1 RE 3000 Ref. 123/457";"160,00";\n';
    

    (Notice the \ns for the line breaks)