Skip to content

Instantly share code, notes, and snippets.

@KANE-99
Last active December 24, 2020 08:05
Show Gist options
  • Save KANE-99/e103b1197e2a02acc65d4a6d7d246179 to your computer and use it in GitHub Desktop.
Save KANE-99/e103b1197e2a02acc65d4a6d7d246179 to your computer and use it in GitHub Desktop.
const {
Parser,
transforms: { flatten, unwind }
} = require("json2csv");
try {
const parser = new Parser({
fields: ["firstColumn", "b.secondColumn", "cars.0.brand", "cars.1.brand"],
transforms: [flatten({ arrays: true }), unwind({ paths: ["cars"] })]
});
const csv = parser.parse({
firstColumn: "first data",
b: {
secondColumn: "second data"
},
cars: [{ brand: "AUDI" }, { brand: "TOYOTA" }]
});
console.log(csv);
// OUTPUT:
// "firstColumn","b.secondColumn","cars.0.brand","cars.1.brand"
// "first data","second data","AUDI","TOYOTA"
} catch (err) {
console.log(err);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment