GithubHelp home page GithubHelp logo

duheng1992 / export-from-json Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zheeeng/export-from-json

0.0 1.0 0.0 330 KB

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

License: MIT License

JavaScript 7.10% HTML 15.29% TypeScript 77.62%

export-from-json's Introduction

Export From JSON

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

Greenkeeper badge 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

Usage

exportFromJSON support CommonJS, EcmaScript Module, UMD importing.

exportFromJSON takes options as the Types Chapter demonstrated, and it uses a front-end downloader as the default processor option. In browser environment, there is a file content size limitation on the default processor, you can consider using a server side solution by passing a custom processor.

In module system

import exportFromJSON from 'export-from-json'

const data = [{ foo: 'foo'}, { bar: 'bar' }]
const fileName = 'download'
const exportType = '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 processor option returns, we can consider such a server side usage for providing 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 '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 here refers to parsable JSON string or a serializable JavaScript object.

Option name Required Type Description
data true Array<JSON> or JSON If the exportType is 'txt' or 'json', data can be any parsable JSON. If the exportType is 'csv' or 'xls', data can only be an array of parsable JSON.
fileName false string filename without extension, default to 'download'
exportType false Enum ExportType 'txt'(default), 'json', 'csv', 'xls'`
processor false (content: string, type: ExportType, fileName: string) => any default to a front-end downloader
withBOM false boolean Add BOM(byte order mark) meta to CSV file. BOM is expected by Excel when reading UTF8 CSV file. It is default to false.

You can also reference these export types through a mounted field types:

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

export-from-json's People

Contributors

zheeeng avatar greenkeeper[bot] avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.