0:00
uh hello guys uh welcome to this video
0:02
So in this video I will show you how to
0:04
export uh the data which is contained
0:06
inside your JSON file into CSV file So
0:10
we have this input uh JSON file right
0:13
here inside our NodeJS application So we
0:16
have an array of objects Each object has
0:18
containing these properties right here
0:20
ID name email age phone number So we
0:24
have these array of objects Almost five
0:27
objects are there And now we need to
0:29
convert this into CSV So I will just run
0:32
this NodeJS script right here And as
0:34
soon as I run this you will see on the
0:36
side The CSV file will be conver uh
0:40
exported The JSON will be converted into
0:43
CSV And if I open this you will see all
0:46
your JSON data is converted into
0:48
commaepparated value which is a CSV file
0:51
And you can actually see the output data
0:56
records has been automatically converted
0:59
So for doing the conversion we are using
1:02
a bit module we third party module So we
1:06
need to install this JSON to
1:09
CSV So just execute this command to
1:14
module So after installing it I will now
1:17
show you step by step how to do this So
1:20
let me delete this CSV file So first of
1:24
all you need to require this module
1:26
alongside the file system module which
1:28
is a built-in module of NodeJS and then
1:31
we require this which is
1:34
your parser method which will be coming
1:38
uh just require it JSON to CSV So we we
1:44
basically require this third party
1:46
package and it contains this function
1:48
parser and now we'll be using this
1:51
function So we simply say parser and we
1:55
construct a new constructor of this
1:58
parser So we require it and then we
2:01
construct a new constructor After that
2:05
say parser dot it contains this function
2:10
parse and here we need to
2:12
specify the data which will be the input
2:17
data So here we just need to create a
2:19
variable of JSON which will contain the
2:23
input data for us So we'll be using the
2:25
JSON.parse function and here we'll be
2:28
reading the content of this JSON file by
2:31
using the file system module File system
2:36
synchronous and uh whatever is
2:39
your JSON file name So my file is
2:42
located in the same directory The name
2:45
of the file is output.json JSON So we
2:48
are specifying it If you see the file is
2:50
located in the same directory So here we
2:53
are specifying it and then parsing
2:56
it in the next step using this parse
2:59
function We specify this
3:01
JSON So then it creates the CSV and now
3:04
we just need to save the CSV file by
3:07
using the file system write
3:11
file synchronously Here we specify the
3:16
output file name which will be output
3:18
dot CSV and then we provide the actual
3:22
data which will be the CSV and then we
3:25
can just write a notification that uh
3:28
CSV file created successfully So these
3:31
are the 11 lines of code guys which is
3:34
required for this application It's very
3:38
script node index.js JS you will see on
3:41
the left hand side the CSV file has been
3:46
created and you can see that so you can
3:49
basically now use this inside your
3:51
NodeJS application to actually convert
3:55
uh JSON file into CSV using this module
3:58
and also check out my website
4:01
freemediatools.com which contains
4:03
thousands of tools regarding audio video