How JSON to CSV Conversion Runs in Your Browser
Converting JSON to CSV is a normalization problem: turn a nested, schema-less tree into a flat rectangle. The pipeline begins with File.text(), which reads the file as a UTF-8 string, and JSON.parse(), the browser's native parser. If the top level is a single object rather than an array, it is wrapped so the code always works on a list of records. The first pass walks every record and collects the union of all keys in first-seen order — that union becomes the header row, so records with heterogeneous fields still collapse into one consistent table.
The second pass builds rows. For each record and each header key, the value is read and normalized: null and undefined become empty cells, while genuinely nested objects or arrays are stringified back to JSON so hierarchical data is preserved inside a single field rather than dropped. Every cell then passes through an RFC 4180 serializer: a field containing a comma, a double quote, or a line break is wrapped in double quotes, and embedded quotes are doubled — the exact convention Excel, Google Sheets, and most database importers expect. Rows are joined with newlines and the header is written first, producing a complete CSV document in one in-memory string.
That string is wrapped in a Blob with the text/csv MIME type and exposed through an object URL for download. Because the whole transform is plain JavaScript with no dependencies, the page is dependency-free and offline-capable; because it never touches a server, even a JSON export containing customer records or proprietary metrics stays on your machine. The operation is also effectively unbounded — the only constraint is the memory needed to hold the parsed object graph, which comfortably covers multi-megabyte API exports on any modern device.