1.7.4 • Published 3 months ago

export-from-json v1.7.4

Weekly downloads
5,677
License
MIT
Repository
github
Last release
3 months ago

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

Known Vulnerabilities Maintainability language license Build Status npm version npm bundle size (minified + gzip) NPM

Installation

yarn add export-from-json

or

npm i --save export-from-json

or

pnpm i --save export-from-json

Usage

exportFromJSON supports CommonJS, EcmaScript Module, UMD importing.

exportFromJSON receives the option as the Types Chapter demonstrated, and it uses a front-end downloader as the default processor. In browser environment, there is a content size limitation on the default processor, consider using the server side solution.

In module system

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

<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:

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 '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.

Option nameRequiredTypeDescription
datatrueArray<JSON>, JSON or stringIf the exportType is 'json', data can be any parsable JSON. If the exportType is 'csv' or 'xls', data can only be an array of parsable JSON. If the exportType is 'txt', 'css', 'html', the data must be a string type.
fileNamefalsestringfilename without extension, default to 'download'
extensionfalsestringfilename extension, by default it takes the exportType
fileNameFormatterfalse(name: string) => stringfilename formatter, by default the file name will be formatted to snake case
fieldsfalsestring[] or field name mapper type Record<string, string>fields filter, also supports mapper field name by passing an name mapper, e.g. { 'bar': 'baz' }, default to undefined
exportTypefalseEnum ExportType'txt'(default), 'css', 'html', 'json', 'csv', 'xls', 'xml'
processorfalse(content: string, type: ExportType, fileName: string) => anydefault to a front-end downloader
withBOMfalsebooleanAdd BOM(byte order mark) meta to CSV file. BOM is expected by Excel when reading UTF8 CSV file. It is default to false.
beforeTableEncodefalse(entries: { fieldName: string, fieldValues: string[] }[]) => { fieldName: string, fieldValues: string[] }[]Given a chance to altering table entries, only works for CSV and XLS file, by default no altering.
delimiterfalse',' \| ';'Specify CSV raw data's delimiter between values. It is default to ,

Tips

  • You can reference these exported types through a mounted static field types, e.g.
exportFromJSON({ data: jsonData, fileName: 'data', exportType: exportFromJSON.types.csv })
  • You can transform the data before exporting by beforeTableEncode, e.g.
exportFromJSON({
    data: jsonData,
    fileName: 'data',
    exportType: exportFromJSON.types.csv,
    beforeTableEncode: rows => rows.sort((p, c) => p.fieldName.localeCompare(c.fieldName)),
})
1.7.4

3 months ago

1.7.3

8 months ago

1.7.2

1 year ago

1.7.1

1 year ago

1.7.0

2 years ago

1.5.4

2 years ago

1.5.3

2 years ago

1.6.0

2 years ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.10

3 years ago

1.3.11

3 years ago

1.3.12

3 years ago

1.3.7

3 years ago

1.3.9

3 years ago

1.3.8

3 years ago

1.3.5

3 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

6 years ago

1.0.19

6 years ago

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago