javascriptimport-from-csvpapaparse

Papa parse cannot parse string but can parse that same string printed to the console


I am running into an extremely stupid and infuriating issue.

I am getting a string from my server which I try to parse with Papa.parse to no avail.

If I print this string to the console by calling it and then copy/pasting that string into Papa.parse, it does work.

Code :

// this does not work
Papa.parse(res.result, {header: true, skipEmptyLines: true}); 

You can see that the \n in the string are not interpreted as linebreak so Papa thinks all values are fields.

enter image description here

res.result

enter image description here

//copy-paste the string printed in the console
a = "the_string_I_just_copied"

enter image description here

Papa.parse(a)

enter image description here

Seems like something happens when I print it in the console because the line return characters are interpreted as line returns but I don't know what to do with that information.


Solution

  • res.result contains the sequence \\n, otherwise your log wouldn't print \n as readable characters, but really just as a new line:

    console.log( "hello\nworld" );
    console.log( "hello\\nworld" );

    This sequence doesn't represent the new line character, and thus it's only normal your parser doesn't recognize it as a row delimiter.

    You need to fix your data.