Two different shapes of data
JSON and CSV are both everyday formats for exchanging data, but they model it differently. CSV (comma-separated values) is flat: a header row names the columns and every following line is one record. JSON is hierarchical: a record can contain nested objects and arrays of any depth. Spreadsheets, BI tools, and database import utilities expect CSV, while APIs and modern applications speak JSON, so converting between the two is a routine task.
The cleanest conversions start from a JSON array of objects that all share the same keys. Each object becomes one row and each key becomes one column. Real data is rarely that tidy, so it helps to understand how a converter deals with the differences.
CSV quoting rules
CSV looks simple, but it has a few rules, described in RFC 4180, that matter as soon as values contain punctuation. A field that contains the delimiter, a double quote, or a line break must be wrapped in double quotes. A double quote inside such a field is escaped by doubling it, so the value She said "hi" is written as "She said ""hi""". Line breaks inside quoted fields are legal, which means one record can span several physical lines.
Splitting a CSV file on commas with a simple text function breaks on exactly these cases. Use a proper parser, and when generating CSV yourself make sure every field that might contain a comma or quote is escaped correctly.
Flattening nested JSON
Because CSV has no nesting, nested objects must be flattened into columns. The usual convention joins the path with a dot, so { "address": { "city": "Pune" } } becomes a column named address.city. Converting back, a converter can rebuild the nested object by splitting column names on the same separator.
Arrays are harder. A short list of simple values can be joined into one cell, for example tags as "red;green". Arrays of objects either become indexed columns such as items.0.sku and items.1.sku, which can produce very wide tables, or need to be exported as a separate table linked by an ID. Pick the approach that matches how the data will be analyzed.
When objects in an array have different keys, the converter collects the union of all keys as columns and leaves cells empty where a record has no value. A sudden explosion in column count is a good hint that the source data is less consistent than expected.
Types, delimiters, and spreadsheet gotchas
CSV stores everything as text. When converting to JSON, type inference turns values that look like numbers or booleans into real JSON numbers and booleans. This is usually helpful, but it can damage identifiers: a ZIP code such as 01234 or a phone number loses its leading zero when treated as a number. Keep such columns as strings.
Not every CSV uses commas. In many European locales the comma is a decimal separator, so spreadsheets export with semicolons instead, and tab-separated TSV files are common for database dumps. Check the delimiter before importing.
Spreadsheet applications add their own surprises. They may reformat long numbers in scientific notation, convert values like 3-4 into dates, and expect a byte order mark to detect UTF-8 correctly. Formula injection is a security concern too: cells starting with =, +, -, or @ can be executed as formulas when opened, so be careful when exporting user-supplied data for others to open in a spreadsheet.