forked from attacomsian/code-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d354711
commit d9c7605
Showing
10 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] |