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.
id,name,role,active
1,John Doe,Admin,true
2,Jane Smith,User,false const data = [
{
"id": 1,
"name": "John Doe",
"role": "Admin",
"active": true
},
{
"id": 2,
"name": "Jane Smith",
"role": "User",
"active": false
}
]; A 200-row CSV becomes a literal you can paste. There is no parser to write and no file to read at runtime.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Yes. Download JS saves the output as a .js file, and Copy JS puts it on the clipboard.
Yes. The literal is valid TypeScript. Add a type annotation, or append as const when you need the exact keys and values typed.
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.