Nodejs json read write and convert
In nodejs json is often used as the standard data serialization language of choice for most applications. When it comes to working with json in general in javaScript there is the JSON.parse, and JSON.stringify methods that are of interest. In addition in nodejs there is the nodejs require global that can be used to quickly load json, and the file system module can be used as a built in way to write files including json. There are alternatives to json such as yaml, but for this post I will be going over some of the basics of json in node.js as a way of handing data serialization and config files.
1 - In nodejs json can be quickly loaded with require
If I just want to simply load in some JSON from an external javaScript nodejs require can be used to do so without having to bother with the file system module or any other kind of additional third party module.
So in a pinch if you know the relative path to the json that you want to load, and the json is property formated json could be parsed just like this.
|
|
Although this might work okay there are still some drawbacks when it comes to error handling at least in the event that the json file is not there, or is not property formated for whatever the reason. So lets take a look at some other options.
2 - So about the possibility of bad json and other errors
When when loading in json in a nodejs project with require there is a possibility that some kind of error might happen in the process. So a common way of going about handling errors with async tasks such as grabbing json these days with javaScript is to use promises.
|
|
This way if an error happens when loading a json file I can write some logic that will do something in the event that an error occurs.
3 - write json
So require can be used to import json but it can not be used to export it. For this something more will need to be used on top of just require. The file system module in node.js can be used to read and write files and do a wide range of additional file io related tasks. So in this section I will be going over a simple example that can be used as a way to write some json to a file using the file system write method and the JSON stringify method.
|
|