CSV and JSON are the two undisputed titans of data interchange.
- CSV (Comma-Separated Values) rules business intelligence, accounting, Excel models, and bulk data exports.
- JSON (JavaScript Object Notation) powers REST APIs, document databases, web applications, and configuration files.
Yet moving data between them is notoriously fraught with bugs: quotation marks get corrupted, numbers with leading zeros (like ZIP codes "01234") turn into numbers (1234), and nested object hierarchies fail to map cleanly onto flat two-dimensional grids.
In this deep-dive guide, we'll examine how to handle bidirectional data transformation correctly according to RFC specifications.
1. The Anatomy of RFC 4180: The Official CSV Standard
Many developers mistakenly believe that parsing CSV is as simple as running line.split(","). This naive assumption breaks on real-world data almost immediately.
Published by the Internet Engineering Task Force (IETF) in 2005, RFC 4180 defines the strict grammar for CSV files:
The 4 Escaping Rules You Must Know
- Fields containing delimiters must be enclosed in double quotes:
If a field contains a comma (
,), the entire field must be quoted:"San Francisco, CA" - Fields containing line breaks must be quoted:
CSV supports multiline values inside a single cell:
"Line 1\nLine 2" - Escaping double quotes:
If a quoted field contains a double-quote character, it must be escaped by prefixing it with an additional double quote (
""):"He said, ""Hello world!""" - Header row: The first record may optionally contain field names corresponding to the columns in the file.
2. Navigating the Pitfalls of Data Type Coercion
Unlike JSON (which natively distinguishes between strings, numbers, booleans, and null), CSV is purely plain text. Everything is a string.
When converting CSV to JSON, a converter must make decisions regarding type coercion:
| Raw CSV Value | Naive Conversion | Safe Coerced Conversion | Watch Out For |
|---|---|---|---|
42 |
"42" (String) |
42 (Number) |
Product SKUs / barcodes where precision matters |
00720 |
720 (Corrupted!) |
"00720" (String) |
US Postal ZIP codes or bank routing numbers |
true |
"true" (String) |
true (Boolean) |
Flag values |
null or empty |
"" (Empty string) |
null |
Database foreign keys |
The Excel Auto-Formatting Trap
If you open a CSV in Microsoft Excel, Excel aggressively "guesses" data types, converting "01234" to 1234 and large integers like credit card numbers into scientific notation (4.532E+15), permanently corrupting data on save.
Always use a dedicated in-browser CSV tool or programmatic pipeline when inspecting raw CSV payloads.
3. How to Flatten Nested JSON into Flat Tables
JSON is a tree structure capable of infinite nesting:
{
"orderId": 9841,
"customer": {
"name": "Elena Rostova",
"location": {
"city": "Berlin",
"country": "Germany"
}
},
"items": ["Laptop", "Mouse"]
}
Spreadsheets (CSV), however, are strictly flat two-dimensional tables (rows and columns).
The Dot-Notation Flattening Technique
To serialize nested objects into columns without losing information, standard ETL pipelines flatten object paths using dot notation:
orderId |
customer.name |
customer.location.city |
customer.location.country |
items |
|---|---|---|---|---|
9841 |
Elena Rostova | Berlin | Germany | ["Laptop","Mouse"] |
Primitive arrays are safely stringified as JSON strings so that no nested children are lost during export.
4. Convert CSV ⇄ JSON ⇄ TSV Securely in Your Browser
Need to transform customer spreadsheets into JSON arrays or flatten nested API responses into clean CSVs without uploading your confidential corporate records to unknown servers?
Use the KitWharf CSV to JSON & TSV Converter:
- Bidirectional Transformation: Seamlessly convert CSV ➔ JSON or JSON ➔ CSV with a single click.
- Smart Delimiter Detection: Automatically detects commas (
,), tabs (\t), semicolons (;), and pipes (|). - Interactive Table Preview: Inspect your parsed records in a scrollable, styled data table with column headers.
- Type Coercion Controls: Toggle number/boolean parsing on or off to preserve leading zeroes.
- Zero Server Storage: Runs 100% locally in your device RAM.