Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptHow to Convert JSON to string in JavaScript ?

How to Convert JSON to string in JavaScript ?

In this article, we are going to learn the conversion of JSON to string in JavaScript. Converting JSON to a string in JavaScript means serializing a JavaScript object or data structure represented in JSON format into a textual JSON string for data storage or transmission.

There are several methods that can be used to Convert JSON to string in JavaScript, which are listed below:

  • Using JSON.stringify() Method
  • Using JSON.stringify() with Indentation
  • Using JSON.stringify() with Replacer Function
  • Using JSON.parse() followed by JSON.stringify() Method

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Using JSON.stringify() Method

In this approach, JSON.stringify() in JavaScript converts JSON data into a formatted string representation.

Syntax:

JSON.stringify(value, replacer, space);

Example: In this example, we are using the above-explained method.

Javascript




const data = { name: "Nikita", age: 21, city: "Noida" };
const result = JSON.stringify(data);
console.log(result);


Output

{"name":"Nikita","age":21,"city":"Noida"}

Approach 2: Using JSON.stringify() with Indentation

In this approach, using JSON.stringify() in JavaScript, specifying optional parameters for indentation to format JSON data into a more readable and structured string representation for debugging or visualization.

Syntax:

const result = JSON.stringify(data, null, 2);

Example: In this example we are using the above-explained approach.

Javascript




const data = { name: "Aman", age: 21, city: "Noida" };
const result = JSON.stringify(data, null, 2);
console.log(result);


Output

{
  "name": "Aman",
  "age": 21,
  "city": "Noida"
}

Approach 3: Using JSON.stringify() with Replacer Function

In this approach, we use JSON.stringify() with a custom replacer function in JavaScript to transform or omit specific values while converting JSON data to a string representation.

Syntax:

const result = JSON.stringify(data, (key, value) => {
if (typeof value === "number") {
// Modify number values
return value * 2;
}
return value;
});

Example: In this example we are using the above-explained approach.

Javascript




const data = { name: "Rahul", age: 30, city: "Delhi" };
const result = JSON.stringify(data, (key, value) => {
    if (typeof value === "number") {
        // Modify number values
        return value * 2;
    }
    return value;
});
console.log(result);


Output

{"name":"Rahul","age":60,"city":"Delhi"}

Approach 4: Using JSON.parse() followed by JSON.stringify() Method

In this approach, we convert JSON string to a JavaScript object using JSON.parse(), then convert the object back to a JSON string using JSON.stringify()

Syntax:

const jsonObject = JSON.parse(str1);
const result = JSON.stringify(jsonObject);

Example: In this example, we Parse str1 into a JavaScript object, store as jsonObject, then convert back to JSON string using JSON.stringify(jsonObject).

Javascript




const str1 = '{"key1":"value1","key2":"value2"}';
const jsonObject = JSON.parse(str1);
const result = JSON.stringify(jsonObject);
console.log(result);


Output

{"key1":"value1","key2":"value2"}
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments