@iwsio/json-csv-node
This package extends @iwsio/json-csv-core specifically targeting Node.JS to support streaming. It's all the same code as before, just moved around. You can read more about it on my blog.
Usage
Buffered (Converts to CSV with an in-memory data source)
const { toCsv, toCsvSync } = require('@iwsio/json-csv-node')
import { toCsv, toCsvSync } from '@iwsio/json-csv-node'
const csv = await toCsv(data, options)
toCsv(data, options, function(err, csv) {...}))
const csv = toCsvSync(data, options)
Streaming (Converts a "row at a time" from a stream source)
When using the streaming API, you can pipe data to it in object mode.
const { toCsvStream } = require('@iwsio/json-csv-node')
import { toCsvStream } from '@iwsio/json-csv-node'
const things = [
{name: 'thing1', age: 20},
{name: 'thing2', age: 30},
{name: 'thing3', age: 45}
]
Readable.from(things)
.pipe(toCsvStream(options))
.pipe(process.stdout)
toCsvStream(options, (err, transform) => {
Readable.from(things)
.pipe(toCsvStream(options))
})
NOTE: All the aliases from previous versions remain intact.
I renamed this API mostly for cosmetics and to cleanup named exports for ES Modules. It made more sense to me to import { toCSV } from 'json-csv'
rather than importing the default and calling its prop. (I've been doing a lot of Typescript lately). But all the original versions are kept intact and tests are in place to assert they work.
const jsonCsv = require('@iwsio/json-csv-node')
import jsonCsv, { toCSV, toCsvStream, buffered, stream } from '@iwsio/json-csv-node'
jsonCsv.buffered
jsonCsv.bufferedSync
jsonCsv.stream
jsonCsv.toCsv
jsonCsv.toCsvSync
jsonCsv.toCsvStream
jsonCsv.csv
jsonCsv.csvBuffered
Options
{
fields :
[
{
name: 'string',
label: 'string',
transform: function(value) { return value; }
}
],
fieldSeparator: ",",
ignoreHeader: false
}
Advanced Example
Here, you can see we're using a deeper set of objects for our source data, and we're using dot notation in the field definitions like: contact.name
for the contact name.
const items = [
{
downloaded: false,
contact: {
company: 'Widgets, LLC',
name: 'John Doe',
email: 'john@widgets.somewhere',
},
registration: {
year: 2013,
level: 3,
},
},
{
downloaded: true,
contact: {
company: 'Sprockets, LLC',
name: 'Jane Doe',
email: 'jane@sprockets.somewhere',
},
registration: {
year: 2013,
level: 2,
},
},
]
const options = {
fields: [
{
name: 'contact.company',
label: 'Company',
},
{
name: 'contact.name',
label: 'Name',
},
{
name: 'contact.email',
label: 'Email',
},
{
name: 'downloaded',
label: "Downloaded",
transform: (v) => v ? 'downloaded' : 'pending',
},
{
name: 'registration.year',
label: 'Year',
},
{
name: 'registration.level',
label: 'Level',
transform: (v) => {
switch (v) {
case 1: return 'Test 1'
case 2: return 'Test 2'
default: return 'Unknown'
}
},
},
],
}
(async () => {
let result = await toCsv(items, options)
console.log(result)
})()
(() => {
let result = toCsvSync(items, options)
console.log(result)
})()
Output
Company,Name,Email,Downloaded,Year,Level
"Widgets, LLC",John Doe,john@widgets.somewhere,pending,2013,Unknown
"Sprockets, LLC",Jane Doe,jane@sprockets.somewhere,downloaded,2013,Test 2