Similar to last week, we’re going to use the immensely popular
To get started, we’ll want to make sure we have
papaparse installed via
npm install --save papaparse # or yarn add papaparse
With the dependency added to our project, we can
require it and get to converting.
papaparse speak, converting from JSON to CSV is called “unparsing”. While I do not love the naming, I do love that the dependency handles both quite well.
To unparse some JSON we can run:
const fs = require('fs'); const Papa = require('papaparse'); const jsonData = [ name: 'Jenny', website: 'http://thatgirljen.com', vlog: 'https://www.youtube.com/channel/UCMw00gabX-9jYq-SWDgij-w', , name: 'Josh', website: 'http://joshtronic.com', vlog: null, , ]; const csvData = Papa.unparse(jsonData, newline: 'n' );
papaparseconverting from JSON to CSV is just as straight forward as going from CSV to JSON!
newline option at the end is optional. If omitted, you’ll end up with lines ending with
rn which may show up as
^M depending on your editor.
Also, I take back what I said about not liking the naming, now that I’ve played with it a bit more, I like the consistency of
parse and then
unparse to go in the opposite direction.
Good stuff? Want more?
Weekly emails about technology, development, and sometimes sauerkraut.
100% Fresh, Grade A Content, Never Spam.