CSV to JS Converter

Paste CSV and get a JavaScript array you can paste into a file. Numbers and booleans come out typed rather than quoted, which is the tedious part to do by hand. The Array of Objects checkbox switches between one object per row and an array of arrays.

Input CSV
JavaScript Output

1 CSV to JavaScript example

Input CSV

1
2
3
id,name,role,active
1,John Doe,Admin,true
2,Jane Smith,User,false

JavaScript Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const data = [
  {
    "id": 1,
    "name": "John Doe",
    "role": "Admin",
    "active": true
  },
  {
    "id": 2,
    "name": "Jane Smith",
    "role": "User",
    "active": false
  }
];

2 What it saves you

Typing

A 200-row CSV becomes a literal you can paste. There is no parser to write and no file to read at runtime.

Type conversion

42 becomes a number and true a boolean, instead of arriving as "42" and "true" and breaking a comparison later. Only values written as JSON numbers convert, so a ZIP code like 00501 and a formatted 1,200 stay strings.

Two output shapes

Use an array of arrays for chart data and headerless files. An array of objects reads better. An array of arrays is smaller and keeps the header line as its first row.

How to use the CSV to JavaScript tool

  1. Put CSV in the left editor: paste it, or use Upload CSV or Load URL. Dummy Data fills in a sample.
  2. Choose the delimiter: comma, semicolon, tab or pipe.
  3. Leave Array of Objects checked to get one object per row keyed by the header. Untick it to get an array of arrays, with the header line kept as the first row.
  4. Output appears on the right, starting with const data =. Cells written as JSON numbers become numbers, and true/false become booleans. 00501 and 0x10 stay strings.
  5. Click Copy JS, or Download JS to save a .js file. Add an export line if another module will import it.

How to convert CSV to a JavaScript array in Node.js and Python

JavaScript (Node.js)

const fs = require("node:fs");

const text = "id,name,score\n1,Ada,98.5\n2,Grace,91\n";

const [headerLine, ...lines] = text.trim().split(/\r?\n/);
const headers = headerLine.split(",");

const cast = (v) => (v !== "" && !isNaN(v) ? Number(v) : v === "true" ? true : v === "false" ? false : v);

const data = lines.map((line) => {
  const cells = line.split(",");
  return Object.fromEntries(headers.map((h, i) => [h, cast(cells[i] ?? "")]));
});

const js = "export default " + JSON.stringify(data, null, 2) + ";\n";
fs.writeFileSync("data.js", js);
console.log(js);

This split-based version is fine for simple files. Use papaparse with { header: true, dynamicTyping: true } when fields may contain quotes or commas.

Python

import csv, io, json

text = "id,name,score\n1,Ada,98.5\n2,Grace,91\n"

def cast(v):
    try:
        return int(v)
    except ValueError:
        try:
            return float(v)
        except ValueError:
            return {"true": True, "false": False}.get(v.lower(), v)

rows = [{k: cast(v) for k, v in row.items()} for row in csv.DictReader(io.StringIO(text))]

with open("data.js", "w", encoding="utf-8") as f:
    f.write("export default " + json.dumps(rows, indent=2) + ";\n")
print(open("data.js").read())

JSON output is valid JavaScript, so json.dumps() is all the serializer you need.

Common errors and how to fix them

SyntaxError: Cannot use import statement outside a module
The importing file uses import syntax in a CommonJS context. Rename it to .mjs or add "type": "module" to package.json. The generated file declares const data without exporting it, so also add export default data; (or module.exports = data; for CommonJS).
TypeScript: Object is possibly 'undefined' or implicit any
The literal has no type. Add an interface and annotate: const data: Row[] = [...], or append as const for exact literal types.
Each row object has one key containing the whole line
The delimiter does not match the file. Semicolon-separated exports from Excel are the usual cause; change the delimiter option.
The first key is "\ufeffid"
The CSV has a UTF-8 byte order mark. Strip it before parsing: text.replace(/^\uFEFF/, "").
Values like 1e3 or 0x10 become numbers
In your own code, isNaN() accepts scientific and hex notation, so those strings are converted. This tool only converts JSON-style numbers, so 0x10 stays a string, while 1e3 still becomes 1000. Use a stricter test such as /^-?\d+(\.\d+)?$/ when the column must stay a string.

Good to know

Embed data as a JavaScript module only for small, fixed tables: country lists, feature flags, test fixtures. The bundler minifies it with the rest of your code, and there is no fetch or parse at runtime. Past a few hundred kilobytes, keep the CSV or JSON as a static asset and load it lazily instead.

Use array of arrays mode for chart libraries that take rows, and for CSVs with no header line. Your only key is then the column index, so keep a constant next to the data that spells out the column order.

Specifications and references

This tool follows the specifications below. Read them when you need the exact normative wording, for example to settle an edge case between two implementations.

Frequently Asked Questions

What does the output look like?

A const declaration holding an array of objects, one per CSV row, with the header names as keys. Add export default data; if another module imports it.

Can I use a different delimiter?

Yes. Choose comma, semicolon, tab or pipe in the dropdown. This page has no custom option, so for any other delimiter use CSV to JSON, which accepts any character and produces output that is also valid JavaScript.

Are numeric values converted to numbers?

Values written as JSON numbers, such as 12, -3.5 or 1e3, become numbers. true and false (in any case) become booleans, and the lowercase text null becomes null. Everything else stays a string, so 00501, 0x10, 1,200 and Infinity keep their exact text and ZIP codes keep their leading zeros.

Can I download the result as a .js file?

Yes. Download JS saves the output as a .js file, and Copy JS puts it on the clipboard.

Is this useful for TypeScript?

Yes. The literal is valid TypeScript. Add a type annotation, or append as const when you need the exact keys and values typed.

Other formats

Use CSV to JSON for plain data rather than code, CSV to PHP array for the backend, and the CSV table viewer to check the file before converting anything.

More Data Converters

Browse all data converters

Copied to clipboard!