Convert between JSON and CSV
JSON is a hierarchical (3D) data structure. A User object can contain an Address object, which can contain an Array of phone numbers.
CSV (Comma-Separated Values), however, is strictly a flat (2D) table of rows and columns. When converting between the two, we face a fundamental dimensional mismatch.
When converting JSON to CSV, the converter has to flatten the tree structure. It does this by creating composite column names using dot-notation.
For example, if a JSON object is { user: { name: "Alice" } }, the resulting CSV column header becomes user.name and the row value is Alice.
Nested objects are easily flattened with dot-notation, but Arrays are notoriously difficult to represent in CSV.
If a user has 3 phone numbers, should the converter create 3 separate rows (duplicating the user's name), or does it create 1 row with a JSON-stringified array in the phone column? This converter uses stringified arrays for nested lists to ensure one JSON object strictly equals one CSV row.
Why is converting nested JSON to CSV inherently difficult?