Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ asyncParser.fromInput(input).toOutput(output).promise()
.catch(err => console.error(err));;
```

you can also use the convenience method `parseAsync` which returns a promise.
you can also use the convenience method `parseAsync` which accept both JSON arrays/objects and readable streams and returns a promise.

```js
const { parseAsync } = require('json2csv');
Expand Down
16 changes: 14 additions & 2 deletions lib/json2csv.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const { Readable } = require('stream');
const JSON2CSVParser = require('./JSON2CSVParser');
const JSON2CSVAsyncParser = require('./JSON2CSVAsyncParser');
const JSON2CSVTransform = require('./JSON2CSVTransform');
Expand All @@ -11,11 +12,22 @@ module.exports.Transform = JSON2CSVTransform;
// Convenience method to keep the API similar to version 3.X
module.exports.parse = (data, opts) => new JSON2CSVParser(opts).parse(data);
module.exports.parseAsync = (data, opts, transformOpts) => {
if (!(data instanceof Readable)) {
transformOpts = Object.assign({}, transformOpts, { objectMode: true });
}

const asyncParser = new JSON2CSVAsyncParser(opts, transformOpts);
const promise = asyncParser.promise();

data.forEach(item => asyncParser.input.push(item));
asyncParser.input.push(null);
if (Array.isArray(data)) {
data.forEach(item => asyncParser.input.push(item));
asyncParser.input.push(null);
} else if (data instanceof Readable) {
asyncParser.fromInput(data);
} else {
asyncParser.input.push(data);
asyncParser.input.push(null);
}

return promise;
};
35 changes: 32 additions & 3 deletions test/JSON2CSVAsyncParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,42 @@ const { Readable, Transform, Writable } = require('stream');
const { AsyncParser, parseAsync } = require('../lib/json2csv');

module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) => {
testRunner.add('should parse json to csv, infer the fields automatically and not modify the opts passed using parseAsync method', (t) => {
testRunner.add('should parse in-memory json array to csv, infer the fields automatically and not modify the opts passed using parseAsync method', (t) => {
const opts = {
fields: ['carModel', 'price', 'color', 'transmission']
};
const transformOpts = { objectMode: true };

parseAsync(inMemoryJsonFixtures.default, opts, transformOpts)
parseAsync(inMemoryJsonFixtures.default, opts)
.then((csv) => {
t.ok(typeof csv === 'string');
t.equal(csv, csvFixtures.default);
t.deepEqual(opts, { fields: ['carModel', 'price', 'color', 'transmission'] });
})
.catch(err => t.notOk(true, err.message))
.then(() => t.end());
});

testRunner.add('should parse in-memory json object to csv, infer the fields automatically and not modify the opts passed using parseAsync method', (t) => {
const opts = {
fields: ['carModel', 'price', 'color', 'transmission']
};

parseAsync({ "carModel": "Audi", "price": 0, "color": "blue" }, opts)
.then((csv) => {
t.ok(typeof csv === 'string');
t.equal(csv, '"carModel","price","color","transmission"\n"Audi",0,"blue",');
t.deepEqual(opts, { fields: ['carModel', 'price', 'color', 'transmission'] });
})
.catch(err => t.notOk(true, err.message))
.then(() => t.end());
});

testRunner.add('should parse streaming json to csv, infer the fields automatically and not modify the opts passed using parseAsync method', (t) => {
const opts = {
fields: ['carModel', 'price', 'color', 'transmission']
};

parseAsync(jsonFixtures.default(), opts)
.then((csv) => {
t.ok(typeof csv === 'string');
t.equal(csv, csvFixtures.default);
Expand Down