TIL: Reading from JSON in NodeJS
I don’t have much time as I’m currently working on my main project for work, but I wanted to take a minute to talk about reading in json files in nodejs.
All of these great info is documented in this original blog post.
For all of these examples, we’ll use a dummy json file with the following content:
|
|
Automatically Reading JSON file
This is probably the absolute easiest way to read in a json file and make it a useable object. All you need to do is simply require the file and assign it to a variable:
|
|
Using file system
The other way is by using the file system fs
module. This requires many more steps for the same result:
|
|
💚