JavaScript JS Tutorial JS Objects JS Functions JS Classes JS Async JS HTML DOM JS Browser BOM JS Web APIs JS AJAX JS JSON JS vs jQuery JS Graphics



JSON Stringify

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. JSON is often used to transmit data between a server and a web application, as an alternative to XML.

JSON.stringify() is a method in JavaScript that converts a JavaScript object or value to a JSON string. The JSON.stringify() method takes two optional parameters: a replacer function and a space argument.

The replacer function can be used to filter out values or transform values before they are included in the JSON string. The replacer function takes two parameters: the key and the value. If the replacer function returns undefined, the value will be excluded from the JSON string. If the replacer function returns any other value, that value will be included in the JSON string.

The space argument is used to add whitespace to the JSON string for readability. The space argument can be a number or a string. If the space argument is a number, that number of spaces will be added to the JSON string. If the space argument is a string, that string will be added to the JSON string.

Here is an example of using JSON.stringify() to convert a JavaScript object to a JSON string:


const person = {
  name: "John",
  age: 30,
  city: "New York"
};

const personJSON = JSON.stringify(person);

console.log(personJSON);
// Output: {"name":"John","age":30,"city":"New York"}

In this example, we create a JavaScript object called person with three properties: name, age, and city. We then use JSON.stringify() to convert the person object to a JSON string and store the result in a variable called personJSON. Finally, we log the value of personJSON to the console.

Here is an example of using JSON.stringify() with a replacer function:


const person = {
  name: "John",
  age: 30,
  city: "New York"
};

const personJSON = JSON.stringify(person, (key, value) => {
  if (key === "name") {
    return value.toUpperCase();
  } else {
    return value;
  }
});

console.log(personJSON);
// Output: {"name":"JOHN","age":30,"city":"New York"}

In this example, we create a replacer function that checks if the key is "name". If the key is "name", the function returns the value in uppercase. If the key is not "name", the function returns the value unchanged. We then use JSON.stringify() with the replacer function to convert the person object to a JSON string and store the result in a variable called personJSON. Finally, we log the value of personJSON to the console.

Here is an example of using JSON.stringify() with a space argument:


const person = {
  name: "John",
  age: 30,
  city: "New York"
};

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

console.log(personJSON);
// Output:
// {
//   "name": "John",
//   "age": 30,
//   "city": "New York"
// }

In this example, we use JSON.stringify() with a space argument of 2 to add two spaces to the JSON string for readability. We then log the value of personJSON to the console.

JSON.stringify() is a powerful method in JavaScript that allows you to convert JavaScript objects and values to JSON strings. With the optional replacer function and space argument, you can customize the output of the JSON string to meet your needs.

References

Activity