The Spreadsheet Was Clean and Well-Organized. The API Could Not Read It. Here Is Why.
The Spreadsheet Was Clean and Well-Organized. The API Could Not Read It. Here Is Why.
The spreadsheet she had inherited contained three years of customer data: names, email addresses, subscription tiers, and renewal dates. It was clean, well-organized, and completely unusable by the new API she needed to integrate with.
The API expected JSON arrays. The spreadsheet was a CSV. She needed to convert.
CSV to JSON is one of the most common data transformation tasks in software development because it sits at a boundary between two different working contexts. Spreadsheets are where non-technical stakeholders produce and organize data. APIs and applications are where developers consume it. CSV and JSON are the native formats on either side of that boundary, and neither side reads the other's format directly.
CSV, which stands for comma-separated values, predates spreadsheet software. The IBM Fortran compiler under OS/360 supported list-directed input and output, using commas between values, in 1972. FORTRAN 77, approved in 1978, formalized this approach in the language standard. The term "comma-separated value" was in use by 1983, several years before the format became familiar to general computer users through spreadsheet software.
The fundamental structure has not changed since those early implementations: values on a line are separated by commas, lines represent records, and the first line optionally contains header names. This simplicity contributed to the format's staying power. Any text editor can read a CSV. Any spreadsheet application can open one. The format requires no special parser and no schema to describe its structure.
CSV's informality was also its limitation. No standard specified how to handle commas within field values, how to handle line breaks within a field, how to indicate character encoding, or whether headers were mandatory. Different implementations made different choices, and files produced by one application frequently failed to import correctly into another.
The Internet Engineering Task Force published RFC 4180 in October 2005, titled "Common Format and MIME Type for Comma-Separated Values (CSV) Files," which codified CSV as an informal standard by documenting common conventions and registering the MIME type text/csv. The W3C followed with a recommendation in 2013 addressing remaining ambiguities. Despite these formalizations, CSV implementations still vary in how they handle edge cases, which is why real-world CSV files often need cleanup before processing.
A CSV file has headers in its first row and values in subsequent rows. When converted to JSON, each row becomes an object and each header becomes a key in that object. The result is a JSON array of objects where every object has the same keys.
The mapping is simple enough that it can be described in one sentence, but the conversion introduces decisions that affect whether the resulting JSON is valid for its intended use.
The most important decision is type inference. CSV stores all values as text. A column named "price" with the value 29.99 is the string "29.99" in the CSV. In JSON, 29.99 should be a number. A column named "active" with the value "true" is a string in CSV. In JSON, it should be the boolean true. A column named "quantity" with the value 0 should be the number 0 in JSON, not the string "0."
Whether conversion produces the correct type depends on the tool. Some tools infer types from content: if a field's values look like numbers, produce numbers; if they look like booleans, produce booleans. Other tools preserve everything as strings, leaving the type coercion to the consuming application. The right behavior depends on the destination: an API that expects a number field and receives a string will fail validation. An application that expects a string and receives a number may also fail. Checking the conversion output before sending it is a necessary step.
Real-world CSV files are rarely as clean as a fresh export from a controlled system. Several issues appear frequently and affect the JSON output.
Empty cells are the most common problem. A record with an empty email field produces a JSON object where the email key maps to an empty string, a null, or is omitted entirely, depending on the converter. APIs that require the email field and do not accept null or empty string will reject records where the cell was blank. If the field is optional, an omitted key may be preferable to a null, but this again depends on the API specification.
Inconsistent date formats are the second frequent issue. A spreadsheet where some dates are formatted as "2024-01-15" and others as "1/15/24" produces inconsistently formatted strings in the JSON output. APIs that expect ISO 8601 dates will accept the first format and reject the second. The CSV-to-JSON conversion cannot fix this; the CSV must be cleaned first.
Leading and trailing whitespace in values is subtle but causes real problems. A value stored as " active" (with a leading space) is different from "active" in any string comparison. Spreadsheet exports sometimes include whitespace around values, particularly after commas where the file was edited manually.
Values containing commas must be enclosed in quotes in a properly formatted CSV. A customer name like "Smith, John" must be written as "Smith, John" with quotation marks, otherwise the comma splits it into two fields. When quotation marks are missing, the converter splits the value incorrectly, shifting all subsequent fields in that row. Detecting this requires inspecting the output for rows where field counts do not match the header count.
CSV is a flat format. Every row has the same columns, every column contains a single value, and there is no mechanism for expressing a value that is itself a collection.
JSON supports nested objects and arrays. A customer record in JSON might include an "orders" key whose value is an array of order objects, each with its own fields. This structure cannot be expressed in a single CSV row. The relational approach is to use multiple CSV files, one per entity, with foreign keys linking them. Converting relational CSV data to nested JSON requires a join operation that a simple converter does not perform.
For data that is naturally flat, the conversion is direct. For data that has been flattened from a relational or hierarchical source, the CSV-to-JSON conversion produces flat JSON objects that may need further transformation to match the structure an API expects.
Understanding whether the destination API expects flat records or nested objects before converting determines whether a simple converter is sufficient or whether additional transformation logic is needed.
A common workflow mistake is converting CSV to JSON and immediately sending the result to an API without inspection. The JSON may be syntactically valid but semantically wrong: correct types for some fields, wrong types for others, missing required fields where the CSV had empty cells, or malformed values from inconsistent CSV formatting.
A practical validation step between conversion and API submission catches these problems before they become failed requests. Most APIs return error messages that identify which field failed and why, but diagnosing those errors is faster with the JSON visible in a readable format than with raw error codes.
For large datasets where manual inspection is impractical, writing a brief validation script that checks field types and the presence of required keys against the API specification is worth the time. The script runs once per conversion, catches systematic problems across the full dataset, and provides a fix target before anything is submitted.
Conclusion
The CSV format was in use on mainframes in 1972, thirty years before the APIs that now consume JSON were built. Its staying power comes from its simplicity: any text editor opens it, any spreadsheet reads it, and any developer can inspect it without tools. The mismatch with JSON is not a design flaw in either format. It is the natural consequence of formats designed for different eras and different contexts meeting at an integration boundary.
ToolHQ's CSV to JSON converter produces a structured JSON array from any CSV file in the browser, handling the header-to-key mapping and providing the output for inspection before it goes anywhere near an API.
Frequently Asked Questions
What does CSV to JSON conversion produce?
Each CSV row becomes a JSON object, with column headers becoming object keys and cell values becoming the corresponding values. The result is a JSON array of objects with identical keys.
Why do data types matter in CSV to JSON conversion?
CSV stores everything as text strings. JSON is typed. If a number stays as a string in the JSON output, APIs expecting numeric types will reject those records.
What happens to empty cells in CSV when converting to JSON?
Depending on the conversion tool, empty cells become empty strings, null values, or are omitted as missing keys. The correct behavior depends on what the consuming API or system expects.