Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape JSON string for CSV parsing?

A bit of an odd question perhaps.

I'm trying to Store a JSON string appropriately in a CSV column as a string. It works OK when generating the CSV file, however parsing the CSV file with the JSON in it is a problem.

I've tried "{"prop": "Val"...}", "{""prop"": ""Val""...}", "{\"prop\": \"Val\"...}", "{\""prop\"": \""Val\""...}"

However none of them parse well at all.

Please help!

like image 463
sidewaiise Avatar asked Jul 18 '18 05:07

sidewaiise


2 Answers

The correct answer here is to double quote the JSON string so this:

{ "a": 10 }

converts into this:

"{ ""a"": 10 }"
like image 132
Martin Avatar answered Oct 25 '22 00:10

Martin


You can use the following replace logic to escape your JSON string:

`"${YOUR_JSON_STR.replace(/\"/g, '""')}"`
like image 45
Harry Yu Avatar answered Oct 25 '22 00:10

Harry Yu