Sitemap
Siddesh Thorat (Software Development Enthusiast)

Siddesh Thorat (Software Development Enthusiast)

Let's explore the world of Software together!!!

JSON.parse in javascript!!!

Hellllooooo everyone,

Today we will be looking into a very simple yet very interesting feature in javascript, YES!!!! correctly guessed its JSON.parse method of javascript

Stayed tuned!!! we will walk through everything that you should know about JSON.parse, Let’s begin!!!

Get Siddesh Thorat (Software Development Enthusiast)’s stories in your inbox

Join Medium for free to get updates from this writer.

What is JSON, in the first place?

  • JSON stands for Javascript Object Notation.
  • Nothing complicated, it is just a key-value pair used to store and transfer data across functions/systems, etc.
  • A simple example is as follows,
Press enter or click to view image in full size
  • JSON is a global object provided by javascript. This object contains static methods for parsing and converting values to JSON i.e an object
  • A JSON object can be invoked without the “new” keyword. All methods and functions associated with JSON objects are static and can be invoked directly without a constructor.
  • JSON object has one static property (JSON[@@toStringTag]) and 2 static methods(parse and stringify) associated with itself, We will be running through parse in detail in this article, Do let me know in the comments section if you wish to read an article on JSON.stringify as well. Having said that, let's continue with the parse method of the JSON object

JSON.parse()

  • JSON.parse simply converts an input string into a JSON object.
  • Following is an example of converting a string into JSON,
Press enter or click to view image in full size
  • The above snippet will result in the following output,
Press enter or click to view image in full size
  • There are certain rules that must be followed while passing input to the parse method else it will throw a syntax error Exception,
  • JSON doesn't allow trailing commas and single quotes.
  • JSON.parse method has a second optional parameter which is simple a function that runs while parsing every value, Check the below example to understand this concept in more detail
Press enter or click to view image in full size

OUTPUT

Press enter or click to view image in full size
  • The second parameter passed to the parse method is called the reviver function, It is invoked when we try to parse the value for any particular key, we can modify the return value as we did above.
  • In the above example, we are checking if the type of value is a number using “typeof” operator, and if true, we are multiplying it by 2, so our final output becomes this { key: ‘one of the value’, key2: 10 }, key2 hold value “10”.
  • This can be used to handle non-callable values like undefined, null, etc
  • An interesting fact is, JSON.parse is not only limited to objects but can also return the following types of values,
  • Array, string, number, boolean, null along with Objects

This is all that you need to start using the parse function efficiently, Do let me know in the comments section if you wish to learn more about this parse function or any other function in Javascript.

Knowledge is power!!!🥇

--

--