@uchina-systems/csv-to-json
v0.1.9
Published
CSV to JSON converter with WASM (Edge/Browser) and Node native add-on
Readme
@uchina-systems/csv-to-json
CSV → JSON converter with a hybrid runtime: WASM for Edge/Browser, and a native N-API add-on for Node. One npm package that “just works” everywhere with the same API.
Install
npm install @uchina-systems/csv-to-jsonWhat you get
- Edge/Browser: WebAssembly build for fast client/edge parsing and validation
- Node: Native add-on for server-side throughput
- Unified API with options, including header handling, delimiter, and optional case normalization
JavaScript/TypeScript usage
The package auto-selects the correct runtime via conditional exports. You can import the same way in Node, Edge, and the browser.
Node (native)
import { parse, parseToArray } from '@uchina-systems/csv-to-json'
import fs from 'node:fs'
const csv = fs.readFileSync('data.csv')
const json = parse(csv, { headers: true, caseStyle: 'pascal' })
console.log(json)
const rows = parseToArray(csv, { headers: true, caseStyle: 'pascal' })
console.log(rows)Edge/Browser (WASM)
import { parse, parseToArray } from '@uchina-systems/csv-to-json'
const bytes = new TextEncoder().encode('YouTube link,IMPORT\nhttps://yt.com,YES\n')
const json = parse(bytes, { headers: true, caseStyle: 'pascal' })
// => [{"YouTubeLink":"https://yt.com","Import":"YES"}]
const rows = parseToArray(bytes, { headers: true, caseStyle: 'pascal' })
// => [{ YouTubeLink: 'https://yt.com', Import: 'YES' }]Next.js examples
Edge route (app/api/parse/route.ts)
export const runtime = 'edge'
import { NextResponse } from 'next/server'
import { parse } from '@uchina-systems/csv-to-json'
export async function POST(req: Request) {
const bytes = new Uint8Array(await req.arrayBuffer())
const json = parse(bytes, { headers: true, caseStyle: 'pascal' })
return NextResponse.json(JSON.parse(json))
}Node route (app/api/parse/route.ts)
export const runtime = 'nodejs'
import { NextResponse } from 'next/server'
import { parse } from '@uchina-systems/csv-to-json'
export async function POST(req: Request) {
const bytes = new Uint8Array(await req.arrayBuffer())
const json = parse(bytes, { headers: true, caseStyle: 'pascal' })
return NextResponse.json(JSON.parse(json))
}API
export type CaseStyle = 'pascal' | 'camel' | 'snake' | 'kebab' | 'title' | 'lower' | 'upper'
export interface ParseOptions {
headers?: boolean // default: true
delimiter?: number // default: 44 (",")
caseStyle?: CaseStyle // optional key normalization (headers only)
}
// Returns JSON string of an array of objects
export function parse(bytes: Uint8Array, options?: ParseOptions): string
// Returns parsed objects directly
export function parseToArray(bytes: Uint8Array, options?: ParseOptions): Array<Record<string, string>>Case normalization
- Examples with caseStyle: 'pascal'
- "YouTube link" → "YouTubeLink"
- "IMPORT" → "Import"
- Mixed-case tokens like "YouTube" are preserved
Browser demo
npm run demo
# Opens http://localhost:5173/examples/browser.html.NET usage (C#)
This repo includes a .NET wrapper and a Rust C ABI. You can use the packaged DLL to call the same parser from C#.
Quick start (local build)
# Build native library for your current platform and copy into runtimes/
node scripts/build-dotnet.mjs
# Create the NuGet package (includes the native library under runtimes/<rid>/native)
dotnet build ./dotnet/Csvjson/Csvjson.csproj -c Release
# or: dotnet pack ./dotnet/Csvjson/Csvjson.csproj -c ReleaseThen reference the produced package (YourScope.Csvjson.0.1.0.nupkg) in your .NET solution, or reference the project directly.
C# example
using System.Text;
using Csvjson;
var csv = "YouTube link,IMPORT\nhttps://yt.com,YES\n";
var bytes = Encoding.UTF8.GetBytes(csv);
var json = Parser.Parse(bytes, headers: true, delimiter: null, caseStyle: "pascal");
Console.WriteLine(json);Notes
- The NuGet currently includes a macOS arm64 native library when built on that platform. To support more RIDs (linux-x64, win-x64, etc.), repeat the native build on those platforms (or cross-compile) and place the outputs under
dotnet/Csvjson/runtimes/<rid>/native/before packing. - NPM package automatically selects WASM (Edge/Browser) or the native add-on (Node) via conditional exports.
Build from source (npm)
npm run buildLicense
MIT OR Apache-2.0
