jtcsv API Documentation
Current version: 3.1.0
Welcome to the jtcsv API documentation. This library provides complete JSON<->CSV bidirectional conversion with streaming support, security features, and TypeScript types.
Quick Links
Installation
bash
npm install jtcsvCore Features
JSON to CSV Conversion
jsonToCsv()- Convert JSON array to CSV stringsaveAsCsv()- Save JSON as CSV filecreateJsonToCsvStream()- Streaming conversion
CSV to JSON Conversion
csvToJson()- Convert CSV string to JSON arrayreadCsvAsJson()- Read CSV file as JSONcsvToJsonIterator()- Async iterator for large filescreateCsvToJsonStream()- Streaming conversion
Format Support
- NDJSON:
jsonToNdjson(),ndjsonToJson(),parseNdjsonStream() - TSV:
jsonToTsv(),tsvToJson(),validateTsv()
Streaming API
All streaming functions support backpressure and work with Node.js streams:
streamJsonToCsv()- Pipe JSON to CSVstreamCsvToJson()- Pipe CSV to JSONcreateNdjsonToCsvStream()- NDJSON to CSV transform stream
Security Features
- CSV injection prevention – automatic escaping of Excel formulas (
=,+,-,@), enabled by default (preventCsvInjection: true) - Path traversal protection – validation of file paths to prevent
../attacks, restricts to.csv/.jsonextensions only - RFC 4180 compliance – strict adherence to CSV standard with proper quote handling and line breaks (
rfc4180Compliant: true) - Automatic row shift repair – detects and corrects misaligned columns caused by missing quotes (
repairRowShifts: true) - Quote normalization – normalizes single quotes, backticks, and smart quotes to standard double quotes (
normalizeQuotes: true) - JSON Schema validation – validate input data against JSON schemas for data integrity
Type Safety
jtcsv provides complete TypeScript definitions:
typescript
import {
jsonToCsv,
csvToJson,
JsonToCsvOptions,
CsvToJsonOptions
} from 'jtcsv';
const options: JsonToCsvOptions = {
delimiter: ',',
preventCsvInjection: true
};
const csv = jsonToCsv(data, options);Error Classes
jtcsv provides specialized error classes for better error handling:
JtcsvError- Base error classValidationError- Invalid input dataSecurityError- Security violationsParsingError- CSV/JSON parsing failuresFileSystemError- File operation errorsLimitError- Exceeded limitsConfigurationError- Invalid options
Performance
jtcsv is optimized for high performance:
- 625,000 rows/sec for simple CSV parsing
- Fast-path engine for common CSV patterns
- Delimiter cache with 3.67x speedup
- Memory-efficient streaming for large files
Complete API Reference
Core Functions
csvToJson()– Convert CSV string to JSON arrayjsonToCsv()– Convert JSON array to CSV stringpreprocessData()– Flatten nested objects before conversiondeepUnwrap()– Internal utility for deep unwrapping
File I/O
readCsvAsJson()– Read CSV file as JSON (async)readCsvAsJsonSync()– Synchronous file readingsaveAsCsv()– Save JSON data as CSV filesaveAsJson()– Save data as JSON filesaveAsJsonSync()– Synchronous JSON save
Streaming
createCsvToJsonStream()– Transform stream CSV→JSONcreateJsonToCsvStream()– Transform stream JSON→CSVstreamCsvToJson()– Pipe CSV stream to JSON streamstreamJsonToCsv()– Pipe JSON stream to CSV streamcreateCsvFileToJsonStream()– Stream CSV file to JSON objectscreateJsonReadableStream()– Create readable stream from JSON arraycreateCsvCollectorStream()– Collect CSV output into buffercreateJsonCollectorStream()– Collect JSON objects into array
NDJSON Support
jsonToNdjson()– Convert JSON array to NDJSON stringndjsonToJson()– Parse NDJSON string to JSON arrayparseNdjsonStream()– Async iterator over NDJSON streamcreateNdjsonToCsvStream()– Transform NDJSON to CSVcreateCsvToNdjsonStream()– Transform CSV to NDJSONgetNdjsonStats()– Statistics about NDJSON data
TSV Support
jsonToTsv()– Convert JSON array to TSV stringtsvToJson()– Parse TSV string to JSON arrayvalidateTsv()– Validate TSV structureisTsv()– Detect if string is likely TSVreadTsvAsJson()– Read TSV file as JSONreadTsvAsJsonSync()– Synchronous TSV file readingsaveAsTsv()– Save JSON as TSV filesaveAsTsvSync()– Synchronous TSV savecreateJsonToTsvStream()– Stream JSON to TSVcreateTsvToJsonStream()– Stream TSV to JSON
Error Handling
JtcsvError,ValidationError,SecurityError,ParsingError,FileSystemError,LimitError,ConfigurationError– Specialized error classescreateErrorMessage()– Create detailed error messageshandleError()– Centralized error handlersafeExecute()– Safe execution wrapper
Utilities
autoDetectDelimiter()– Auto-detect CSV delimitervalidateFilePath()– Security validation for file pathsisEmail(),isUrl(),isDate()– Built-in validatorscreateBatchProcessor()– Batch processing helperasyncIterUtils– Utilities for async iterationdetectEncoding(),convertToUtf8()– Encoding utilities
For complete details, see the generated API documentation.
Browser Support
jtcsv works in browsers with Web Workers support:
javascript
import { jsonToCsv, csvToJson } from 'jtcsv/browser';See the individual API documentation below for detailed usage information.