Read/Write JSON Files with Node.js
ฝัง
- เผยแพร่เมื่อ 8 ก.ย. 2024
- See the written version of this tutorial for links to prerequisites and related tutorials: heynode.com/tu...
When you want to store data between server restarts with Node, JSON files are a simple and convenient choice. Whether you are reading a config file or persisting data for your application, Node has some built-in utilities that make it easy to read and write JSON files. Using JSON files in your app can be a useful way to persist data. We will look at a few different methods for working with JSON files.
In this tutorial we'll:
- Read JSON data from disk
- Learn to use fs module to interact with the filesystem
- Persist data to a JSON file
- Use JSON.parse and JSON.stringify to convert data to and from JSON format
Heynode.com: The best online Node.js courses and tutorials to help you solve hard problems fast.
An excellent tutorial. And it's exactly what I was looking for as I am about to embark on writing a simple editor for a very specific json file. I will be the sole user and the file is not terribly large, so I am not concerned about multiple users editing the file at the same time. And it's great that you have a written version of this for reference. Thank you!
Bro.... What a teaching skill.... Cleared all doubts in single video👍
I was searching for json read write whole day then found your vedio.......
Just 10 min but explained every thing😍
Your voice is sooo soothing
but vs code white theme is killing me
Thank you for the tutorial, it's very informative ))) Please consider using your IDE with a dark theme when making your videos, it's hard to watch this at night 😅
Finally after zapping over 2 tons of inconsistent fs module tutorials, i got he good one ! Damn it
Very clearly explained, thanks for the help! I had trouble trying to push a new item into an array in a json document, and this cleared some things up for me
what happens if you actually have 2 or 3 json objects in the file? how do you handle to update them? can you share it with me?
Best tutorial ever.. Keep up the great work!
Thank you for this great video.
Very good tutorial, consice and easy to understand! Thank you so much! 💛
You explained really clearly. Nice tutorial. Great work!
I've been wandering for hours now, looking for JSON read/file help in Node.js (newbie here). Loading JSON faking a web request/response just wasn''t for me and for the project I'm currently on, yet it seemed the only, or at least the "way more popular" way (why in the world? you already possess the *.json file locally... just wondering). I can't really describe how big is my wish to say "Thank you". Thank you for existing, this video is exactly what I needed.
Hey, just want to thank you for making this video tutorial. Cheers
Thanks for the wonderful explanation. Helped me alot.
Thanks! Nice explaination
Underrated channel
Very clearly and simply explained. Thankyou.
Super easy example help nobody in practice
You explained it quite clearly, thank you!
Very nice example
Thanks amazing tutorial gained a new sub
Thanks for the sub!
Very nice.
Please make us learn the same thing in Typescript as well 🙏
Splendid stuff.
Really nice tutorial, thank you!
Best Tutorial ever MADE
Nice tutorial, interested to watch you covering other topics too
Hey thanks Omar. What other topics in particular would you like to see?
@@heynode how do you actually handle this if you have 2 or more json object?
Thank you bro, helped me alot.
very good information. Thanks for sharing.
Thank you very much!
great video. Thank you!
Great tutorial. Thanks a ton.
Great and just great
Thanks for your help :)
Great video, thanks!
thank you help a lot !
07:00 fs.writeFile
감사합니당
You're the best!
Nice tutorial, just hated the light theme
Tutorial starting time show require file then without require system, you are show data??
How can we parse chunks as valid JSON? What if we have a huge json file and want to make some logic for chunks?
Good video ! (why you use light mode? lmao )
i am having problem in reading and writing it is saying file not exists only way i was able to read it using require() can any one help me
may i know how to convert a text file to json in nodejs
How we can add data in json file without erasing/deleting the exist data on the json file
good job!
i need to put a type: module in json file but don't know how to do it
Can this NODE be used like js, without libraries buddy?
🔥🔥🔥🔥 Thanks
tout ça c'est beau mais comment on fait pour utiliser les données dans un vrai programme javascript?
This error - ReferenceError: JSONReader is not defined at Object. - on Update code. :(
The function jsonReader() was defined earlier in the video at th-cam.com/video/HrjC6RwEpt0/w-d-xo.html, you'll need to make sure that function exists before writing the update code. Hope that helps.
perfect
is fetch() not a good move to use to get data from json file?
I think you would want to use fetch() if the .json file was served over HTTP and you had to request it from another server. And fs.readFile for files on the same filesystem.
does fs.readFile always read the current state of the file BECAUSE it's an async function?... or are those 2 separate pieces of information?
Two different bits of information. The important piece here is that using something like `require('./file.json')` loads the file's content into the node.js memory cache and it's possible that subsequent calls will return the cached result instead of reading the file from disk again. `fs.readFile` will always ready the file from disk whenever it is executed.
Hey thank you for these videos, is there a function i could write to send json data to my email ?
I've not done it myself, but it looks like you could accomplish this with nodemailer.com/usage/. Using something like the jsonReader() function in this tutorial, and then instead of console.log() to output the data write a function that uses ndoemailer to send an email that contains the data you've read in.
@@eojthebrave thanks Joe
@An A Count lol
How to do same with react native?
ReferenceError: jsonReader is not defined
EDIT: Nvm i just looked at tutorial on google(your video on website)
How do you do this on react?
But on build this is not working
i am not able to get address
good game
tip, use dark mode
Yeah lol
are you kiding with public?
I keep getting an error. Javascript is possible the worst programming language.