JSON parse and JSON stringify with JavaScript Code

JSON.stringify

JSON.stringify is a method in JavaScript used to convert a JavaScript object into a JSON string. JSON stands for JavaScript Object Notation and is a lightweight data interchange format used for exchanging data between systems.

Here are some examples of using JSON.stringify:

Converting a JavaScript object to a JSON string:

const person = {

  name: “John Doe”,

  age: 30,

  city: “New York”

};

const jsonString = JSON.stringify(person);

console.log(jsonString); // Output: “{“name”:”John Doe”,”age”:30,”city”:”New York”}”

Replacing values with a custom function:

const person = {

  name: “John Doe”,

  age: 30,

  city: “New York”

};

const jsonString = JSON.stringify(person, (key, value) => {

  if (key === “city”) return “Unknown”;

  return value;

});

console.log(jsonString); // Output: “{“name”:”John Doe”,”age”:30,”city”:”Unknown”}”

Excluding properties:

const person = {

  name: “John Doe”,

  age: 30,

  city: “New York”

};

const jsonString = JSON.stringify(person, [“name”, “age”]);

console.log(jsonString); // Output: “{“name”:”John Doe”,”age”:30}”

Specifying the number of spaces for indentation:

const person = {

  name: “John Doe”,

  age: 30,

  city: “New York”

};

const jsonString = JSON.stringify(person, null, 2);

console.log(jsonString); 

/* Output: 

{

  “name”: “John Doe”,

  “age”: 30,

  “city”: “New York”

}

*/

Here are 10 examples of using JSON.stringify in JavaScript:

Converting a simple object to a JSON string:

const obj = {name: “John”, age: 30};

const jsonString = JSON.stringify(obj);

console.log(jsonString); // Output: ‘{“name”:”John”,”age”:30}’

Replacing values with a custom function:

const obj = {name: “John”, age: 30};

const jsonString = JSON.stringify(obj, (key, value) => {

  if (key === “age”) return value + 10;

  return value;

});

console.log(jsonString); // Output: ‘{“name”:”John”,”age”:40}’

Excluding properties:

const obj = {name: “John”, age: 30, city: “New York”};

const jsonString = JSON.stringify(obj, [“name”, “age”]);

console.log(jsonString); // Output: ‘{“name”:”John”,”age”:30}’

Specifying the number of spaces for indentation:

const obj = {name: “John”, age: 30, city: “New York”};

const jsonString = JSON.stringify(obj, null, 2);

console.log(jsonString); 

/* Output:

{

  “name”: “John”,

  “age”: 30,

  “city”: “New York”

}

*/

Converting an array to a JSON string:

const arr = [1, 2, 3];

const jsonString = JSON.stringify(arr);

console.log(jsonString); // Output: ‘[1,2,3]’

Converting a nested object to a JSON string:

const obj = {name: “John”, age: 30, address: {city: “New York”, state: “NY”}};

const jsonString = JSON.stringify(obj);

console.log(jsonString); // Output: ‘{“name”:”John”,”age”:30,”address”:{“city”:”New York”,”state”:”NY”}}’

Converting a date object to a JSON string:

const date = new Date();

const jsonString = JSON.stringify(date);

console.log(jsonString); // Output: ‘”Wed Jan 01 2030 00:00:00 GMT+0000 (Coordinated Universal Time)”‘

Converting a function to a JSON string:

const obj = {name: “John”, age: 30, getName: function() {return this.name}};

const jsonString = JSON.stringify(obj);

console.log(jsonString); // Output: ‘{“name”:”John”,”age”:30}’

Converting an object with circular references to a JSON string:

const obj = {name: “John”, age: 30};

obj.self = obj;

const jsonString = JSON.stringify(obj);

console.log(jsonString); // Output: ‘{“name”:”John”,”age”:30,”self”:”[Circular]”}’

JSON.parse 

JSON.parse is a method in JavaScript that parses a JSON string and returns a JavaScript object. It takes a string as an argument and converts it into a JavaScript object. This method is commonly used to parse data received from an API or stored in a file.

Here’s an example of how to use JSON.parse:

const jsonString = ‘{“name”: “John”, “age”: 30}’;

const obj = JSON.parse(jsonString);

console.log(obj.name); // Output: “John”

console.log(obj.age); // Output: 30

In this example, the JSON.parse method is used to parse the JSON string ‘{“name”: “John”, “age”: 30}’ into a JavaScript object obj. The properties of the object can then be accessed as usual, for example obj.name and obj.age.

examples of using JSON.parse method in JavaScript:

Parsing a simple JSON string:

const jsonString = ‘{“name”: “John”, “age”: 30}’;

const obj = JSON.parse(jsonString);

console.log(obj.name); // Output: “John”

console.log(obj.age); // Output: 30

Parsing a JSON string with multiple levels of nesting:

const jsonString = ‘{“name”: “John”, “age”: 30, “address”: {“city”: “New York”, “state”: “NY”}}’;

const obj = JSON.parse(jsonString);

console.log(obj.address.city); // Output: “New York”

Parsing a JSON string with an array:

const jsonString = ‘{“name”: “John”, “age”: 30, “languages”: [“English”, “Spanish”]}’;

const obj = JSON.parse(jsonString);

console.log(obj.languages[0]); // Output: “English”

Parsing a JSON string with a date:

const jsonString = ‘{“name”: “John”, “age”: 30, “birthday”: “1990-01-01”}’;

const obj = JSON.parse(jsonString, (key, value) => {

  if (key === “birthday”) return new Date(value);

  return value;

});

console.log(obj.birthday instanceof Date); // Output: true

Parsing a JSON string with a custom reviver function:

const jsonString = ‘{“name”: “John”, “age”: 30}’;

const obj = JSON.parse(jsonString, (key, value) => {

  if (key === “age”) return value + 10;

  return value;

});

console.log(obj.age); // Output: 40

Parsing a JSON string with a toJSON method:

const jsonString = ‘{“name”: “John”, “age”: 30, “toJSON”: function() {return {“name”: this.name}}}’;

const obj = JSON.parse(jsonString);

console.log(obj.name); // Output: “John”

Parsing a JSON string with a circular reference:

const jsonString = ‘{“name”: “John”, “age”: 30, “self”: {“$ref”: “#”}}’;

try {

  const obj = JSON.parse(jsonString);

} catch (e) {

  console.log(e); // Output: “TypeError: Converting circular structure to JSON”

}

Parsing a JSON string with NaN, Infinity, and -Infinity:

const jsonString = ‘{“name”: “John”, “age”: 30, “income”: NaN, “debt”: -Infinity, “assets”: Infinity}’;

const obj = JSON.parse(jsonString);

console.log(obj.income === NaN); // Output: true

console.log(obj.debt === -Infinity); // Output: true

console.log(obj.assets === Infinity); // Output: true

Parsing a JSON string with a bigInt: