JSON methods

JSON (JavaScript Object Notation) is a data-interchange format. It is easy for humans to read and write. sometimes we want to convert it into a String, for example when we want to send it over a network.

we could implement his conversion like this :

let person = {
  name: "max",
  lastname: "mustermann",
  toString() {
    return `{name: "${}", lastname: ${this.lastname}}`;

alert(person); // {name :"john", lastname : "mustermann"}

But in the Development Process, new Properties will be added, old one will be removed or updated, such toString() method every time is not the best idea. we could loop over Properties , but the problem is when our object is nested so we will have to implement their conversion too.

Happily, There is no need to handle all this, This Task is already solved with stringify()

Javscript provides two methods when it comes to JSON :

  • JSON.stringify()to convert a JavaScript object to a JSON string.
  • JSON.parse() parses a JSON string, constructing the JavaScript object described by the string.


As we said before when sending data to a web server, the data has to be a string. Here is an example with stringify() :

Imagine we have this object in JavaScript:

let object = { name: "max mustermann", age: 30 };

This is how we convert it into a String :

var myJSON = JSON.stringify(obj);

The result will be a string following the JSON notation.


When receiving data from a web server, the data is always a string.

Parse the data with JSON.parse() and it will become a JavaScript object.

for example :

var obj = JSON.parse('{ "name":"max mustermann", "age":30}');

Maher Gallaoui

You like our Content? Follow us to stay up-to-date.