How to save data to JSON file

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so i'm just going to go over how to write data to a json file i've just got set up a rest api and it's using express um the first part of the video is just going to be creating a function to save our data and the second part will be adapting that function so we can pass in different javascript objects and save it to different json files the first thing that we're going to want to do is just grab hold of our fs modules so fs require fs we're going to want to create our function um i'm just going to call it save data and then at the moment i'm going to pass through dog the javascript uh dog objects because that's what i'm going to be saving the first time um so we've got our javascript object and then we want to change that to a into json format and we do that with json.stringify and then we pass through our dog object we're going to want to bind this to a variable i'm going to call that const json data and then just so you can see the difference if i console dot log both of these oh not console 11 console.log and the first console log will be our javascript object and the second one will be our json data just going to call our function and passing dog and run this in the terminal so as you can see the first line we've got our javascript object and then the second line we've got our javascript object javascript object but it's in a json format and this is the data that we're going to want to be saving so oh i do know the shortcut to that um so in order to save this data we need to use fs and the first argument that we're going to be passing through is where are we saving this so i'm going to be saving the data to dogs.json to dog.json the file doesn't exist but it will just create the file for me the second argument is what data do we want to save so i want to save my json data and finally the third argument is uh you pass through a uh a function um which will catch the ever and it will prevent the uh save from happening so i'm gonna call the function finished um within the save dog function i'm going to create another function called finished and i'm going to want to pass through ever so if and ever occurs so if ever i want to see this ever so console.ever and then heather and finally return so what return does is if there is an error it prevents the data from being saved so it just it completely backs it backs you out of the fs.5 file and then you can go to your console see whatever is and try and fix it so there we go we've got our function um it catches any ever and it prevents the save from happening if there is an error it changes our javascript object into a json format and then it saves so we've already got it the function called at the bottom if i restart my server you'll see the json file being created and then within this json file we've got our json data it's not in a very nice format so if we go down to json.stringify and we pass in two more um arguments we the second one can be null and the third one can be two if i restart my server you'll see that the actual format of this json file is so much more nicer and more readable it's very helpful if you've got a lot of data so as i say in the second part of this video is just going to be me changing this function so you can just constantly call the same function but save different job should save different javascript objects to different json files because if we wanted to save our javascript cat object instead of rewriting this whole object again what we can do is if we pass through and say the file so data file so the data here that we want to save is the uh at that time it was the dog object but if i just change that to data and then the same for the file here so the file that we was originally doing was dog.json but if i pass file there and then in our save data function the first argument would be the data which is dog and then the second one is the file which is uh dog.json but let's just um for now uh say i don't know um second dog talk jason and then we want to call the function again so save data and we want to save our cat so we want to pass in the cat object and let's call it i don't know very creative cats.json let's make sure it's in a string format cat.json and if we just write our reload our server as you can see we've got our cat.json our cat.json file created with our data saved as well and the same with our second dog.json it's got the same data but it's just in a different file so that's it
Info
Channel: Chascript
Views: 14,749
Rating: 4.8540144 out of 5
Keywords: save javascript object to json file, change javascript object to json format, json.stringify, fs.writefile, how to write data to json file, javascript, how to save javascript data, save javascript data, chascript
Id: T7s3st6xfpA
Channel Id: undefined
Length: 7min 0sec (420 seconds)
Published: Wed Feb 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.