jehiah / json2csv

command line tool to convert json to csv
MIT License
801 stars 94 forks source link

Strings are not escaped #2

Closed thinkjson closed 12 years ago

thinkjson commented 12 years ago

If I have the source JSON

{"message": "this is a \"test quote\""}

then when I run json2csv -k message <JSON, the output looks like

"this is a "test quote""

which breaks any CSV reader. The escaping used in the JSON should be carried over to the CSV.

thinkjson commented 12 years ago

Affected lines, as far as I can tell: https://github.com/jehiah/json2csv/blob/master/json2csv.c#L78-80

It looks like you're fetching the unescaped string, and then printing that straight to the CSV file. You could probably get away with using the JSON escape function (https://github.com/jehiah/json-c/blob/master/json_object.c#L74) as the rules are similar enough.

thinkjson commented 12 years ago

I found a work-around that also fixes numbers and nulls. I've sent a pull request.