The use of the method is fairly straight forward just call the method off of the JSON object and pass a JSON string to parse into an object, the returned object is then the workable object from that json string. The JSON.stringify method works in a similar way, only the first argument passed should be the object that I want to convert into a JSON string.
For starters in this section I will be going over the basics of the JSON.parse method. This will include just using the method on a simple string of JSON, error handling, and other basic example of the JSON parse method that should work okay in most environments that support JSON.
In addition there is also the reviver method argument that is a way to go about setting a function that can be used to set values for the final object that is returned. This can sometimes lead to more complex examples of the JSON.parse method, but I will still be going over a quick simple example of it here.
The JSON parse method can be used to parse a JSON string into a workable object by just passing the JSON string as the first argument. The workable object will then be returned by the method, assuming that nothing goes wrong, which can then be stored in a variable.
Here I am using a STring literal that just happens to be valid JSON, but in a real example this JSON string would be obtained by reading a file, or receiving a HTTP request body that that is a JSON string. The JSON parse method is just simply a way to convert this kind of string to an object.
In certain projects where the json parse is being used to parse from a source that possible give invalid json it is possible for an error to happen. So it is often generally a good idea o use the json parse method in a try catch statement, and then have a way to handle such errors.
A reviver method can be given as a second argument to the json parse method after giving the string to parse. This method will have a key and value argument, and the returned value will become the new value in the resulting object returned by json parse.
the use of this method might come in handy if the JSON code has a bunch of values that need to be used to create new instances of a class or something to that effect.
Of course when it comes to using require as a way to parse and load JSON into a script in nodejs just like with the JSON.parse method an error can happen in the event that the file is not there, or of the file is there but it is not JSON, or a file is there and it is JSON but it is malformed. So just like with the JSON.parse method it is a goo idea to use require in the body of a try block so that I can run some code in the event that there is an error.
There is also having ways to go about setting what the path of the JSON file is by way of something other that a string literal. That alone will work fine as long as the file is relative to the current working directory, and not the script. In which case even when it comes to using a literal often I might still want to use path methods and certain globals to help get a file relative to the script rather than the current working directory. However for this example alone at least I am making it so that I can use positional arguments to set the location of the JSON file, and only default to a hard coded value in the event that a positional argument is not given. These positional arguments are values that I can pass when calling a script with the node binary in the command line.
The way to go about accessing any and all positional arguments that where given when the script was called would be to look at the argv array of the process object. In this array for first element of index zero will be the nodejs binary, next will then be the name of the script, so it is element index 2 that will be the first positional argument. In the event that there is no given positional the default value will be undefined which will evaluate to false, so I can then use the || operator to go with a hard coded value for the file name. In any case the value that is given to the resolve method of the nodejs built in path module to make sure that the end result is resolved to an absolute rather than relative path.
This nodejs example will check for a JSON file in the current users home dir, if the file is there or there is not any other kind of error that happens reading the file for some reason, then the JSON.parse method will be used to parse the json string into an object. If there is an error parsing the JSON then the next catch statement will fire, the error code for file not found will not be in the error object so things will just continue to the last catch statement where the error will be logged to the standard error and thats it. This way I will only take action if the file is not there, and not overwrite a malformed json file in the event of a syntax error.
So this nodejs example makes use of the read file, and write file methods of the file system module. I am also using the home dir method of the os module to get the path to the home folder in an operating system independent kind of way, and I am using the join method of the path module as a way to handling the joining and parsing of path names. The promsify method of the utils method is a way to make sure that I am working with file system methods that will return a promise, in late versions of node this will happen all ready so it is just a backward support thing.
So then here I have a basic example of the JSON parse method where I am reading a JSON file, parsing the JSON file into an object, and updating the state of that object. Once I have an updated state of the object I am then using the JSON.stringify method to create a string from that object, and then write this new state back to the file.
For this example I am creating a text area element that will default to some hard coded JSON. I can then use this text area element to mutate the state of this JSON, and when I do so the inner text of another element will change to inform me that the JSON is valid or not.
So then for this example I am using the document.querySelector method as a way to gain references to the text area element as well as the div element that I will be using to update the inner text of depending on the state of the JSON in the text area element. I then have a parse helper method that will use the JSON.parse method to parse a given text string, in the event that it is valid JSON the resulting object will be returned, else if something goes wrong the error object will be what is returned by the method. With that said I then also have a is valid helper method that will call the parse method and check the constructor of the returned object to see if it is a Syntax Error or not, in the event that it is the method returns false, else the method will return true. Finally I have a JSON check method that will pass the current value of the text area element as the text to check if it is vailid json or not, in the event that it is the inner text of the display method will give a message that everything is okay, else it will give another message.
So that is it for now when it comes to the JSON parse method. There is way more to write about when it comes to the use of the JSON parse method when it comes to some real code examples maybe. There is also of course the JSON.stringify method that is also worth mentioning when it comes to converting a workable object to a JSNON string for example.
If I get some time to come around to updating this post again I might add some additional examples of both parsing and stringify objects with JSON. That may or may not happen as I have some many other posts, and projects that are more deserving of my attention.