Data Guides

CSV to JSON for APIs: Delimiters, Encoding and Data Types

Convert CSV rows into JSON objects locally, then handle delimiters, quoted fields, duplicate headers, encoding and string-to-type decisions correctly.

  • Published
  • 4 min read
  • Updated
CSV rows converted into formatted JSON objects with strings preserved
A real CSV fixture converted by the production data engine. The output was parsed again and checked for a quoted comma, embedded line break and UTF-8 text.

CSV looks simple until it contains commas inside text, embedded line breaks, empty cells, duplicate headers, leading zeros or values that resemble numbers and booleans. JSON adds another decision: should 001 be a number, an identifier string or invalid data? A reliable converter must parse the table first and avoid inventing types it cannot know.

The AnyFileConvertor CSV-to-JSON path turns the first row into object keys and preserves every cell as a string. That is intentionally conservative. Type conversion belongs in a reviewed mapping for the destination API.

The input and output shape

Given this CSV:

Name,Note,Count
Ada,"said ""hello"",
then left",2
Lin,éclair & <pie>,0

the converter produces an array of objects:

[
  {
    "Name": "Ada",
    "Note": "said \"hello\",\nthen left",
    "Count": "2"
  },
  {
    "Name": "Lin",
    "Note": "éclair & <pie>",
    "Count": "0"
  }
]

Notice that Count remains a string, the comma and line break stay inside Ada's quoted field, and the UTF-8 accented character survives. The converter cannot know whether an API expects a number, identifier, Boolean or free text.

The real conversion test

The evidence run selected an actual UTF-8 .csv file in the production page, completed the conversion, read the JSON Blob and parsed it with JSON.parse. Assertions checked row count, the quoted comma, the embedded line break and the accented character.

CheckRecorded result
Input size75 B
Output size166 B
Rows2 JSON objects after the header
Quoted comma and newlinesaid "hello",\nthen left stayed in one value
UTF-8 and data typeéclair survived and Count stayed a string
BrowserMicrosoft Edge 156.0.4314.42

How delimiter detection works

The parser inspects the first non-empty line and counts commas, tabs, semicolons and pipe characters outside quoted fields. It chooses the delimiter with the highest count, defaulting to comma when none appears. This handles common CSV, TSV-like, semicolon and pipe-delimited exports without a setting.

Automatic detection is not omniscient. A misleading first line or a malformed quote can choose the wrong structure. Inspect the JSON keys and first few rows. If a source has an unusual delimiter, normalise it in a trusted spreadsheet or text tool before conversion.

Quoted fields, line breaks and escaped quotes

A quoted field may contain the delimiter or a line break. Two consecutive quotes inside a quoted field represent one literal quote. The parser rejects an unclosed quoted field and non-whitespace characters that appear after a closing quote before the delimiter.

Those checks prevent a malformed row from being silently split into plausible but wrong objects. When a conversion fails, inspect the row near the first unmatched quote rather than deleting quotation marks indiscriminately.

Blank, duplicate and uneven headers

The first row supplies object keys. A blank heading becomes Column N. Duplicate headings are made unique in order: the second occurrence receives (2), the third (3), and so on. Extra cells beyond the header width are not turned into invented keys; short rows receive empty strings for missing header positions.

For an API import, fix headings deliberately before conversion. Stable machine keys such as customer_id are safer than labels with spaces or duplicated names. The converter preserves a table; it does not design an API schema.

Encoding and byte-order marks

The browser's File.text() path reads the CSV as UTF-8, and the tabular parser removes a leading UTF-8 byte-order mark character. A legacy Windows-1252 or UTF-16 CSV can therefore show replacement characters or fail to match expected values.

Export UTF-8 from the source application when possible. If the file is legacy encoded, use the Encoding operation in Text Tools first, then reopen names containing accents or non-Latin characters before trusting the whole dataset.

Convert data types only with explicit rules

Before sending JSON to an API, define a field map. For example, convert active to a Boolean only when its value is exactly an allowed token; parse quantity as an integer with a range check; retain account numbers and postal codes as strings; and decide whether an empty cell means an empty string, null or a missing property.

Do not run a blanket "looks numeric" conversion. It can turn 001 into 1, lose precision from long identifiers or reinterpret dates according to locale. JSON formatting cannot repair a mistaken data model.

Convert and validate step by step

  1. Export the source as UTF-8 CSV and keep the original.
  2. Open CSV to JSON and choose one file up to the documented 50 MB table limit.
  3. Run the conversion and download the formatted JSON array.
  4. Use Validate on the JSON Formatter, Minifier & Validator, then inspect keys and sample rows.
  5. Apply an explicit type and nullability mapping in code or a reviewed data pipeline.
  6. Test the result against a staging API before a production import.

Privacy and scale limits

The data engine parses the file locally and does not need to upload customer rows or API data. It still reads the full source and builds the full JSON string in memory. A 50 MB CSV can expand substantially as objects repeat every header key.

For very large or regulated datasets, use a streaming local script with schema validation, audit logs and controlled access. The browser tool is best for reviewed, manageable files and quick transformations where its conservative string mapping is understood.

CSV JSON APIs delimiters data types

Tools used in this guide

These links match the article examples. Review each tool's tested inputs and output limits before conversion.

Convert CSV rows to JSON objects locally. The first row supplies keys and cells remain strings; common delimiters are detected. Free, no data upload or signup.

CSV JSON

Export a JSON array of objects as CSV locally. Columns use the combined keys; nested values become JSON text. Free, no signup or upload. Review spreadsheet imports.

JSON CSV

Format readable JSON, create compact JSON or validate JSON syntax locally with useful parser errors. Free, no upload or signup; not JSON Schema validation.

Process JSON

CSV Tools

Spreadsheets

Extract selected CSV columns by normalized header name or 1-based position, preview the result and download CSV locally. Links to retained CSV utilities included.

Extract CSV columns