Gathering detailed insights and metrics for @json2csv/plainjs
Gathering detailed insights and metrics for @json2csv/plainjs
Gathering detailed insights and metrics for @json2csv/plainjs
Gathering detailed insights and metrics for @json2csv/plainjs
Flexible conversion between JSON and CSV
npm install @json2csv/plainjs
Typescript
Module System
Node Version
NPM Version
99.8
Supply Chain
99.6
Quality
77.7
Maintenance
100
Vulnerability
100
License
Updated on 05 Dec 2024
TypeScript (94.51%)
JavaScript (5.49%)
Cumulative downloads
Total Downloads
Last day
32%
Compared to previous day
Last week
-1.9%
Compared to previous week
Last month
4.4%
Compared to previous month
Last year
255%
Compared to previous year
2
Fast and highly configurable JSON to CSV converter. It fully support conversion following the RFC4180 specification as well as other similar text delimited formats as TSV.
@json2csv/plainjs
exposes plain JavasScript modules of json2csv
which can be used in Node.js, the browser or Deno.
This package includes two modules:
There are multiple flavours of json2csv:
Parser
API and a new StreamParser
API which doesn't the conversion in a streaming fashion in pure js.Node Transform
and Node Async Parser
APIs for Node users.WHATWG Transform Stream
and WHATWG Async Parser
APIs for users of WHATWG streams (browser, Node or Deno).CLI
interface.And a couple of libraries that enable additional configurations:
transforms
for json2csv (unwind and flatten) allowing the using to transform data before is parsed.formatters
for json2csv (one for each data type, an excel-specific one, etc.). Formatters convert JSON data types into CSV-compatible strings.You can install json2csv as a dependency using NPM.
1$ npm install --save @json2csv/plainjs
You can install json2csv as a dependency using Yarn.
1$ yarn add --save @json2csv/plainjs
json2csv plainjs modules is packaged as an ES6 modules. If your browser supports modules, you can load json2csv plainjs modules directly on the browser from the CDN.
You can import the latest version:
1<script type="module"> 2 import { Parser } from 'https://cdn.jsdelivr.net/npm/@json2csv/plainjs/src/Parser.js'; 3 import { StreamParser } from 'https://cdn.jsdelivr.net/npm/@json2csv/plainjs/src/StreamParser.js'; 4</script>
You can also select a specific version:
1<script type="module"> 2 import { Parser } from 'https://cdn.jsdelivr.net/npm/@json2csv/plainjs@6.0.0/src/Parser.js'; 3 import { StreamParser } from 'https://cdn.jsdelivr.net/npm/@json2csv/plainjs@6.0.0/src/StreamParser.js'; 4</script>
json2csv
can be used programmatically as a synchronous converter.
This loads the entire JSON in memory and do the whole processing in-memory while blocking Javascript event loop. For that reason is rarely a good reason to use it until your data is very small or your application doesn't do anything else.
1import { Parser } from '@json2csv/plainjs'; 2 3try { 4 const opts = {}; 5 const parser = new Parser(opts); 6 const csv = parser.parse(myData); 7 console.log(csv); 8} catch (err) { 9 console.error(err); 10}
fields
<DataSelector[]> Defaults to toplevel JSON attributes.transforms
<Transform[]> Array of transforms to apply to the data. A transform is a function that receives a data recod and returns a transformed record. Transforms are executed in order.formatters
<Formatters> Object where the each key is a Javascript data type and its associated value is a formatters for the given type.defaultValue
<Any> value to use when missing data. Defaults to <empty>
if not specified. (Overridden by fields[].default
)delimiter
<String> delimiter of columns. Defaults to ,
if not specified.eol
<String> overrides the default OS line ending (i.e. \n
on Unix and \r\n
on Windows).header
<Boolean> determines whether or not CSV file will contain a title column. Defaults to true
if not specified.includeEmptyRows
<Boolean> includes empty rows. Defaults to false
.withBOM
<Boolean> with BOM character. Defaults to false
.See https://juanjodiaz.github.io/json2csv/#/parsers/parser.
The synchronous API has the downside of loading the entire JSON array in memory and blocking JavaScript's event loop while processing the data. This means that your server won't be able to process more request or your UI will become irresponsive while data is being processed. For those reasons, it is rarely a good reason to use it unless your data is very small or your application doesn't do anything else.
The async parser processes the data as a it comes in so you don't need the entire input data set loaded in memory and you can avoid blocking the event loop for too long. Thus, it's better suited for large datasets or system with high concurrency.
The streaming API takes a second options argument to configure objectMode
and ndjson
mode. These options also support fine-tunning the underlying JSON parser.
The streaming API support multiple callbacks to get the resulting CSV, errors, etc.
1import { StreamParser } from '@json2csv/plainjs'; 2 3const opts = {}; 4const asyncOpts = {}; 5const parser = new StreamParser(opts, asyncOpts); 6 7let csv = ''; 8parser.onData = (chunk) => (csv += chunk.toString()); 9parser.onEnd = () => console.log(csv); 10parser.onError = (err) => console.error(err); 11 12// You can also listen for events on the conversion and see how the header or the lines are coming out. 13parser.onHeader = (header) => console.log(header); 14parser.onLine = (line) => console.log(line);
ndjson
<Boolean> indicates that the data is in NDJSON format. Only effective when using the streaming API and not in object mode.fields
<DataSelector[]> Defaults to toplevel JSON attributes.transforms
<Transform[]> Array of transforms to apply to the data. A transform is a function that receives a data recod and returns a transformed record. Transforms are executed in order.formatters
<Formatters> Object where the each key is a Javascript data type and its associated value is a formatters for the given type.defaultValue
<Any> value to use when missing data. Defaults to <empty>
if not specified. (Overridden by fields[].default
)delimiter
<String> delimiter of columns. Defaults to ,
if not specified.eol
<String> overrides the default OS line ending (i.e. \n
on Unix and \r\n
on Windows).header
<Boolean> determines whether or not CSV file will contain a title column. Defaults to true
if not specified.includeEmptyRows
<Boolean> includes empty rows. Defaults to false
.withBOM
<Boolean> with BOM character. Defaults to false
.Options used by the underlying parsing library to process the binary or text stream.
Not relevant when running in objectMode
.
Buffering is only relevant if you expect very large strings/numbers in your JSON.
See @streamparser/json for more details about buffering.
stringBufferSize
<number> Size of the buffer used to parse strings. Defaults to 0 which means to don't buffer. Min valid value is 4.numberBufferSize
<number> Size of the buffer used to parse numbers. Defaults to 0 to don't buffer.See https://juanjodiaz.github.io/json2csv/#/parsers/stream-parser.
See LICENSE.md.
No vulnerabilities found.
No security vulnerabilities found.