7

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!

sidewaiise
  • 1,445
  • 3
  • 16
  • 27

2 Answers2

5

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

{ "a": 10 }

converts into this:

"{ ""a"": 10 }"
Martin
  • 660
  • 10
  • 23
2

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

`"${YOUR_JSON_STR.replace(/\"/g, '""')}"`
Harry Yu
  • 323
  • 1
  • 3
  • 11