JSON to CSV Conversion: Nested Objects, Flattening, and Escaping
Learn how JSON arrays become CSV rows, how nested objects are flattened, and where RFC 4180 escaping changes the result.
Key takeaways
- A CSV row is flat, so nested JSON objects need an explicit column naming strategy before export.
- RFC 4180 requires quoting when a field contains a delimiter, quote, or line break, and quotes inside a quoted field are doubled.
- Array values need a documented representation because CSV has no native nested-array type.
- A local converter can keep the working payload in browser memory, but large files still need a documented size limit.
Companion utility
Try JSON to CSV Converter
Use the browser utility for the same task, then return here for the implementation details.
1. The direct answer
JSON to CSV conversion works by first deciding the column model. A JSON array of objects maps naturally to rows and columns. A single object becomes one row. Nested objects require a flattening rule, and arrays require a serialization rule. Without those decisions, a converter can produce valid JSON and invalid expectations in the spreadsheet.
2. Flattening nested objects
A common column strategy uses dot notation. The object { user: { name: 'Rana' } } becomes the column user.name. This keeps related values together without losing the original path. The strategy should be documented because different systems use different conventions for nested data.
function flatten(value, prefix = '', result = {}) {
for (const [key, item] of Object.entries(value)) {
const path = prefix ? prefix + '.' + key : key;
if (item && typeof item === 'object' && !Array.isArray(item)) {
flatten(item, path, result);
} else {
result[path] = Array.isArray(item) ? item.join('|') : item;
}
}
return result;
}3. RFC 4180 escaping
CSV is a text format, not a spreadsheet-specific object format. A field containing a comma, quote, or line break must be quoted. A quote inside a quoted field is written as two quotes. Spreadsheet software can also interpret a leading equals sign, plus sign, or minus sign as a formula, so exported values need a deliberate formula-safety policy.
4. Browser limits and verification
The ToolNara converter keeps the working value in browser memory and applies a 15 MB input guard. That is a safety boundary, not a guarantee of unlimited processing. Test nested objects, empty arrays, Unicode, embedded commas, line breaks, and spreadsheet formula prefixes before importing a production export.
Rana Jawad
Principal Systems Architect at ToolNara. The guides focus on browser runtime behavior, standards, and engineering trade-offs.
View author profile