I’m trying to convert JSON arrays to CSV files in C++ and I’m curious what the method should be.
Let’s say I have a JSON array that looks like this:
[["Bcode","firstname","lastname"],
["11234","richard","nixon"]]
and I want to convert it to a .csv that, (as I understand it) would be
Bcode,firstname,lastname,
11234,richard,nixon
The JSON array I’m getting seems to already have a line break in it, so can I just delete all the quotation marks and brackets and have it be a workable CSV file?
Alternately, is there any file extension that I can save JSON arrays to that can be read by MSExcel?
Thanks
There’s no reason to remove the quote marks — CSV can (and frequently does) include them. They’re necessary anytime you might want to include a comma inside a field (e.g., if you had a “name” field instead of
firstnameandlastnameas separate fields, the value might be: “Nixon, Richard”. If you delete the quotes, it’ll no longer look like a single field.You do want to delete the comma at the end of the line.
I would also consider any field that includes an embedded quote. If memory serves, JSON uses
\"to signal a quote inside a field. CSV uses"", so unless your data doesn’t/won’t include any embedded quotes, you may need to deal with replacing\"with""as well.