Skip to content

Commit

Permalink
add json to csv conversion tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
attacomsian committed Apr 9, 2020
1 parent d354711 commit d9c7605
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 0 deletions.
32 changes: 32 additions & 0 deletions nodejs/json-to-csv/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.gradle
/build/
!gradle/wrapper/gradle-wrapper.jar

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### Node ###
node_modules

# Extras
/uploads/
3 changes: 3 additions & 0 deletions nodejs/json-to-csv/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# How to convert JSON to CSV in Node.js

For step-by-step instructions, please visit the [blog post](https://attacomsian.com/blog/nodejs-convert-json-to-csv).
22 changes: 22 additions & 0 deletions nodejs/json-to-csv/app-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// require json-2-csv module
const converter = require('json-2-csv');
const fs = require('fs');

// read JSON from a file
const todos = JSON.parse(fs.readFileSync('todos.json'));

// convert JSON array to CSV string
(async () => {
try {
const csv = await converter.json2csvAsync(todos);

// print CSV string
console.log(csv);

// write CSV to a file
fs.writeFileSync('todos.csv', csv);

} catch (err) {
console.log(err);
}
})();
20 changes: 20 additions & 0 deletions nodejs/json-to-csv/app-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// require json-2-csv module
const converter = require('json-2-csv');
const fs = require('fs');

// read JSON from a file
const todos = JSON.parse(fs.readFileSync('todos.json'));

// convert JSON array to CSV string
converter.json2csv(todos, (err, csv) => {
if (err) {
throw err;
}

// print CSV string
console.log(csv);

// write CSV to a file
fs.writeFileSync('todos.csv', csv);

});
17 changes: 17 additions & 0 deletions nodejs/json-to-csv/app-promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// require json-2-csv module
const converter = require('json-2-csv');
const fs = require('fs');

// read JSON from a file
const todos = JSON.parse(fs.readFileSync('todos.json'));

// convert JSON array to CSV string
converter.json2csvAsync(todos).then(csv => {

// print CSV string
console.log(csv);

// write CSV to a file
fs.writeFileSync('todos.csv', csv);

}).catch(err => console.log(err));
35 changes: 35 additions & 0 deletions nodejs/json-to-csv/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// require json-2-csv module
const converter = require('json-2-csv');
const fs = require('fs');

// declare a JSON array
const todos = [
{
"id": 1,
"title": "delectus aut autem",
"completed": false
},
{
"id": 2,
"title": "quis ut nam facilis et officia qui",
"completed": false
},
{
"id": 3,
"title": "fugiat veniam minus",
"completed": false
}];

// Convert JSON array to CSV string
converter.json2csv(todos, (err, csv) => {
if (err) {
throw err;
}

// print CSV string
console.log(csv);

// write CSV to a file
fs.writeFileSync('todos.csv', csv);

});
27 changes: 27 additions & 0 deletions nodejs/json-to-csv/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions nodejs/json-to-csv/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "json-to-csv",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"json-2-csv": "^3.6.2"
}
}
4 changes: 4 additions & 0 deletions nodejs/json-to-csv/todos.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
id,title,completed
1,delectus aut autem,false
2,quis ut nam facilis et officia qui,false
3,fugiat veniam minus,false
16 changes: 16 additions & 0 deletions nodejs/json-to-csv/todos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[{
"id": 1,
"title": "delectus aut autem",
"completed": false
},
{
"id": 2,
"title": "quis ut nam facilis et officia qui",
"completed": false
},
{
"id": 3,
"title": "fugiat veniam minus",
"completed": false
}
]

0 comments on commit d9c7605

Please sign in to comment.