# export-from-json

> Export JavaScript and JSON data to txt, json, csv, tsv, xls, xml, css, and html files.

Latest version **1.8.2** (published 2026-08-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install export-from-json
pnpm add export-from-json
yarn add export-from-json
bun add export-from-json
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.8.2 |
| Published | 2026-08-24 |
| First published | 2018-02-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 118.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 179 |
| Author | Zheeeng |
| Maintainers | zheeeng |
| Keywords | json, csv, tsv, xls, xml, excel, typescript |

## Links

- npm: https://www.npmjs.com/package/export-from-json
- Repository: https://github.com/zheeeng/export-from-json
- Homepage: https://github.com/zheeeng/export-from-json#readme
- Issues: https://github.com/zheeeng/export-from-json/issues
- npm.io page: https://npm.io/package/export-from-json

## Dependencies (1)

- [tslib](https://npm.io/package/tslib.md) ^2.6.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.8.2 (latest) — 2026-08-24
- 1.8.1 — 2026-07-22
- 1.8.0 — 2026-07-22
- 1.7.4 — 2023-12-28
- 1.7.3 — 2023-07-25
- 1.7.2 — 2023-02-15
- 1.7.1 — 2023-02-12
- 1.7.0 — 2022-09-14
- 1.6.0 — 2022-03-24
- 1.5.4 — 2022-03-24
- 1.5.3 — 2022-03-21
- 1.5.2 — 2022-02-10
- 1.5.1 — 2022-01-20
- 1.5.0 — 2022-01-20
- 1.4.1 — 2021-09-27
- … 36 more at https://npm.io/package/export-from-json/versions

## README

<h1 align="center">Export From JSON</h1>

<div align="center">

Export to plain text, css, html, json, csv, tsv, xls, xml files from JSON.

[![Known Vulnerabilities](https://snyk.io/test/github/zheeeng/export-from-json/badge.svg)](https://snyk.io/test/github/zheeeng/export-from-json)
[![Maintainability](https://api.codeclimate.com/v1/badges/2fbc35f65ba61bc190e1/maintainability)](https://codeclimate.com/github/zheeeng/export-from-json/maintainability)
[![language](https://img.shields.io/badge/%3C%2F%3E-TypeScript-blue.svg)](http://typescriptlang.org/)
[![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/zheeeng/export-from-json/blob/main/LICENSE)
[![Build Status](https://travis-ci.org/zheeeng/export-from-json.svg?branch=main)](https://travis-ci.org/zheeeng/export-from-json)
[![npm version](https://img.shields.io/npm/v/export-from-json.svg)](https://www.npmjs.com/package/export-from-json)
[![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/export-from-json.svg)](https://unpkg.com/export-from-json/dist/umd/index.min.js)
[![NPM](https://nodei.co/npm/export-from-json.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/export-from-json/)

</div>

## Installation

```sh
yarn add export-from-json
```

or

```sh
npm i --save export-from-json
```

or

```sh
pnpm i --save export-from-json
```

## Usage

`exportFromJSON` supports CommonJS, EcmaScript Module, UMD importing.

`exportFromJSON` receives the option as the [Types Chapter](#types) demonstrated, and it uses a [front-end downloader](https://github.com/zheeeng/export-from-json/blob/main/src/processors.ts) as the default processor. In browser environment, there is a **content size limitation** on the default processor, consider using the [server side solution](#in-nodejs-server).

### In module system

```javascript
import exportFromJSON from 'export-from-json'

const data = [{ foo: 'foo'}, { bar: 'bar' }]
const fileName = 'download'
const exportType =  exportFromJSON.types.csv

exportFromJSON({ data, fileName, exportType })
```

### In browser

Check the [codepen example](https://codepen.io/zheeeng/pen/PQxBKr)

```javascript
<script src="https://unpkg.com/export-from-json/dist/umd/index.min.js"></script>
<script>
    const data = [{ foo: 'foo'}, { bar: 'bar' }]
    const fileName = 'download'
    const exportType = 'csv'

    window.exportFromJSON({ data, fileName, exportType })
</script>
```

### In Node.js server

`exportFromJSON` returns what the option `processor` returns, we can use it on server side for providing a converting/downloading service:

```javascript
const http = require('http')
const exportFromJSON = require('export-from-json')

http.createServer(function (request, response){
    // exportFromJSON actually supports passing JSON as the data option. It's very common that reading it from http request directly.
    const data = '[{"foo":"foo"},{"bar":"bar"}]'
    const fileName = 'download'
    const exportType = 'txt'

    const result = exportFromJSON({
        data,
        fileName,
        exportType,
        processor (content, type, fileName) {
            switch (type) {
                case 'txt':
                    response.setHeader('Content-Type', 'text/plain')
                    break
                case 'css':
                    response.setHeader('Content-Type', 'text/css')
                    break
                case 'html':
                    response.setHeader('Content-Type', 'text/html')
                    break
                case 'json':
                    response.setHeader('Content-Type', 'text/plain')
                    break
                case 'csv':
                    response.setHeader('Content-Type', 'text/csv')
                    break
                case 'tsv':
                    response.setHeader('Content-Type', 'text/tab-separated-values')
                    break
                case 'xls':
                    response.setHeader('Content-Type', 'application/vnd.ms-excel')
                    break
            }
            response.setHeader('Content-disposition', 'attachment;filename=' + fileName)
            return content
        }
    })

    response.write(result)
    response.end()
}).listen(8080, '127.0.0.1')
```

## Types

**Note:** `JSON` refers to a parsable JSON string or a serializable JavaScript object.
For `json`, `csv`, `tsv`, `xls`, and `xml` exports, string input is parsed as JSON. For `txt`, `css`, and `html`
exports, string input is exported as-is.

For `csv`, `tsv`, and `xls` exports, parsed input must be an array of plain objects (each row must be a non-null,
non-array object). Primitives, `null` values, arrays, and class instances in row positions are rejected.

TSV exports always use a tab delimiter, the `.tsv` extension, and the `text/tab-separated-values` media type.
For compatibility with common spreadsheet tools, fields containing tabs are encoded with the same double-quote
extension used by the CSV encoder. Strict IANA TSV consumers may reject embedded tabs even when quoted.

The legacy `xls` export contains an HTML table that spreadsheet applications can open; it is not a binary BIFF
workbook or an XLSX archive. Consumers that require a native workbook format should convert the exported table
with a dedicated spreadsheet library.

XML exports do not include a `DOCTYPE`. When a source field name must be normalized into a valid XML element
name, the original key is preserved in an escaped `name` attribute. Circular references are rejected with the
path at which the cycle was found.

| Option name | Required | Type | Description
| ----------- | -------- | ---- | ----
| data        | true     | `Array<JSON>`, `JSON` or `string` | If the exportType is 'json', data can be any JSON value. If the exportType is 'csv', 'tsv', or 'xls', data can only be an array of parsable JSON. If the exportType is 'txt', 'css', or 'html', the data must be a string type.
| fileName    | false    | string | filename without extension, default to `'download'`
| extension    | false    | string | filename extension, by default it takes the exportType
| fileNameFormatter    | false    | `(name: string) => string` | Filename formatter. By default, every run of whitespace is replaced with `_`.
| fields      | false    | `string[]` or field name mapper type `Record<string, string>` | Select and optionally rename fields. Declaration order determines output column order; every selected column is retained even when all values are missing. Output field names must be unique.
| exportType  | false    | Enum ExportType | 'txt'(default), 'css', 'html', 'json', 'csv', 'tsv', 'xls', 'xml'
| processor   | false    | `(content: string, type: ExportType, fileName: string) => any` | default to a front-end downloader
| withBOM     | false    | boolean | Add BOM(byte order mark) metadata to CSV or TSV files. BOM is expected by `Excel` when reading UTF-8 files. It defaults to `false`.
| beforeTableEncode     | false    | `(entries: TableEntries) => TableEntries` | Alter columns before encoding CSV, TSV, or XLS data. Field names must be unique and every string-value column must retain the original row count.
| delimiter   | false    | `',' \| ';'` | Specify the CSV delimiter. It defaults to `,` and does not override TSV's fixed tab delimiter.
| escapeFormulae   | false    | boolean | Prefix formula-like CSV or TSV cell values with a single quote to reduce spreadsheet formula injection risk. Field names are not modified. It defaults to `false`.

### Tips

* TypeScript consumers can import `ExportType`, `IOption`, `TableRow`, `TableEntry`, and `TableEntries`
  directly from the package.

* CSV and TSV exports render rows directly by default to reduce intermediate memory. Supplying
  `beforeTableEncode` uses the column-oriented transform path because the callback operates on complete columns.

* You can reference these exported types through a mounted static field `types`, e.g.

```js
exportFromJSON({ data: jsonData, fileName: 'data', exportType: exportFromJSON.types.csv })
```

* You can transform the data before exporting by `beforeTableEncode`, e.g.

```js
exportFromJSON({
    data: jsonData,
    fileName: 'data',
    exportType: exportFromJSON.types.csv,
    beforeTableEncode: rows => rows.sort((p, c) => p.fieldName.localeCompare(c.fieldName)),
})
```

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md) for development, verification, and release instructions.

---
_Source: https://npm.io/package/export-from-json · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
