JavaScript Convert JSON Object to Object String

JavaScript JSON to string; Through this tutorial, i am going to show you how to convert JSON objects to JSON strings in javascript.

How to Convert Javascript JSON Object to JSON String

Using the javascript JSON.stringify() method, you can convert json object a JSON string.

Syntax of json.stringfy method

JSON.stringify(value[, replacer[, space]]);
  • value:- It’s a required parameter and converts to JSON string.
  • replacer:- it is an optional parameter that determines how object values are stringified for objects. It can be a function or an array of strings.
  • space: It is also an optional parameter. This argument is used to control spacing in the final string generated using JSON.stringify() function.

Example 1 – Convert Object to JSON String JavaScript

Let’s see the following example for convert the JSON objects to JSON strings in javascript using json.stringfy() method; as follows:

var myObj = {
  name: 'Developer',
  age: 25,
  favoriteColor: 'Black'
};
var myObjStr = JSON.stringify(myObj);
document.write('Result of the above code is:-' + myObjStr);

Example 2 – Convert any date objects into strings

Let’s see the following example to convert any date objects into strings using the JSON.stringify() method; as follows:

var myObj = {
  name: 'Developer',
  age: 25,
  favoriteColor: 'Black',
  today: new Date(),
};
var myObjStr = JSON.stringify(myObj);
document.write('Result of the above code is:-' + myObjStr);

More JavaScript Tutorials

Leave a Comment