export-from-json
Convert JavaScript objects and JSON into downloadable txt, csv, tsv, xls, xml, css, html, or json files, in the browser or on the server.
Repository Health
Technical Analysis
export-from-json is a small, dependency-light TypeScript utility that turns JSON or plain JavaScript data into a downloadable file in one of eight formats: txt, css, html, json, csv, tsv, xls, and xml. It ships a single exportFromJSON function that accepts the data, a target export type, and a set of formatting options, and hands the result to a pluggable processor — a front-end downloader by default, or a custom function for streaming a response on a Node.js server.
The library focuses on correctness for tabular exports: CSV and TSV output follows RFC-style quoting and escaping rules (commas, quotes, and line breaks are enclosed and escaped), an optional escapeFormulae flag guards against spreadsheet formula injection, and a fields option lets callers select, rename, and reorder columns. XML export normalizes invalid element names while preserving the original key as an attribute, and rejects circular references with a precise path to the cycle. The legacy xls format renders an HTML table that spreadsheet software can open, rather than a binary workbook.
What You Get
- A single
exportFromJSONfunction supporting eight export types: txt, css, html, json, csv, tsv, xls, and xml - CommonJS, ESM, and UMD builds so it works in bundlers, Node.js, and directly via a
<script>tag - RFC-style CSV/TSV encoding with correct quoting, escaping, and an
escapeFormulaeoption to block formula-injection payloads - A
fieldsoption for selecting, renaming, and reordering table columns, including abeforeTableEncodehook for full column-level transforms - Pluggable
processorfunction so the same conversion logic can trigger a browser download or stream a response from a Node.js HTTP server - Full TypeScript typings for
ExportType,IOption,TableRow,TableEntry, andTableEntries
Common Use Cases
- Exporting a data table to CSV/XLS - a dashboard or admin panel lets users download the current table view as a spreadsheet-compatible file
- Downloading API responses as JSON files - a debugging or support tool saves a JSON payload to disk with one call
- Server-side export endpoints - a Node.js HTTP handler uses the
processoroption to set content-type headers and stream CSV/TSV/XLS content back to the client - Configuration or report snapshots - an internal tool exports settings or generated reports as human-readable txt/html files
- Interchange with legacy or non-JS systems - structured data is exported as XML with normalized element names for systems that expect an XML feed
Under The Hood
Architecture
The library is organized as a small set of single-purpose modules: exportFromJSON.ts is the public entry point that validates options, dispatches on exportType, and delegates to converters.ts for format-specific encoding and to processors.ts for the pluggable output step (a browser downloadFile by default, or any custom function for server use). types.ts centralizes the ExportType union and table-shape types, and utils.ts holds small, dependency-free helpers (assertions, key/entry extraction, filename normalization, HTML/XML escaping) reused across the converters. This separation keeps the format-conversion logic (pure functions operating on data) fully decoupled from the side-effecting download step, so the same core can run in a browser or on a server simply by swapping the processor.
Tech Stack
Written in TypeScript and compiled to three targets — ESM (dist/esm), an ES2015 build, and a UMD bundle (dist/umd) built with Rollup and rollup-plugin-typescript2 plus @rollup/plugin-terser for minification. The only runtime dependency is tslib, keeping the published package minimal. Type declarations are emitted separately via tsc, and the package exposes conditional exports map entries for types/import/require so consumers on either module system resolve the correct build. Development tooling is pnpm-based, with a Rollup dev-watch script and a manual test page under example/.
Code Quality
Testing uses Jest with ts-jest in ESM mode, and the __tests__/ directory contains one spec file per exported function (CSV, XLS, XML, field mapping, table map/entries construction, filename normalization, processors) plus dedicated CSV/TSV round-trip specs and snapshot tests — twelve spec files in total. ESLint runs with typescript-eslint’s recommended rules and --max-warnings 0 as part of npm test, and CI (GitHub Actions) runs the full test-and-build pipeline across Node 22 and 24, plus a CodeQL security scan and a post-build verify-package script that checks the published artifact. Error handling is explicit: invalid data, non-unique field names, mismatched row counts, and circular references all throw descriptive Errors via a shared assert helper rather than failing silently.
What Makes It Unique
Most JSON-to-file exporters handle a single format or leave escaping edge cases (formula injection, circular XML references, invalid XML element names) to the caller. export-from-json treats these as first-class concerns: it enclosures the original key as an XML attribute when normalizing invalid names, tracks a full traversal path when reporting circular references, and offers an explicit escapeFormulae flag specifically for spreadsheet formula-injection mitigation — a security-adjacent detail rarely addressed in comparable libraries.