
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Simple Node.JS CSV export module that can export a rich JSON array of objects to CSV.
I've decided to consolidate this package under my @iwsio scope as a new package named @iwsio/json-csv-node
. This was in part inspired by the lack of support for browser due to the streaming API usage like stream.Readable
. Version 5 is 100% backwork compatible with v4. You can read more about it on my blog.
Checkout @iwsio/json-csv-node for the update!
const jsoncsv = require('json-csv')
const csv = await jsoncsv.buffered(data, options) //returns Promise
//optionally, you can use the callback
jsoncsv.buffered(data, options, (err, csv) => {...}))
When using the streaming API, you can pipe data to it in object mode.
const jsoncsv = require('json-csv')
const readable = some_readable_source //<readable source in object mode>
readable
.pipe(jsoncsv.stream(options)) //transforms to Utf8 string and emits lines
.pipe(something_else_writable)
})
{
//field definitions for CSV export
fields :
[
{
//required: field name for source value
name: 'string',
//required: column label for CSV header
label: 'string',
//optional: transform value before exporting
transform: function(value) { return value; }
}
],
// Other default options:
fieldSeparator: ","
,ignoreHeader: false
,encoding: "utf8"
}
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.
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', // uses dot notation
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 function writeCsv() {
try {
let result = await csv.buffered(items, options)
console.log(result)
} catch (err) {
console.error(err)
}
}
writeCsv()
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
FAQs
Easily convert JSON array to CSV in Node.JS via buffered or streaming.
The npm package json-csv receives a total of 2,330 weekly downloads. As such, json-csv popularity was classified as popular.
We found that json-csv demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.