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
Recommended:-JavaScript LocalStorage
Recommended:-JavaScript IP Address Validation Example
Recommended:-Highlight Searched text on a page with in Javascript
Recommended:-JavaScript Encode and Decode URL
Recommended:-JavaScript Functions Example Tutorial
Recommended:-JavaScript Anonymous Function
Recommended:-JavaScript Callback Functions with Example
Recommended:-JavaScript Arrow Function es6 Example
Recommended:-JavaScript Promise with Example
Recommended:-JavaScript: Object Properties
Recommended:-20+ JavaScript Array Methods
Recommended:-20 + JavaScript String Methods
Recommended:-JavaScript Compare Two Arrays for Matches
Recommended:-JavaScript Get Unique Values From Array
Recommended:-JavaScript Clone Array Method with Example
Recommended:-JavaScript Remove First, Last Element From Array
Recommended:-JavaScript Remove Duplicate From Array of Object
Recommended:-Extract Elements From Array in JavaScript
Recommended:-JavaScript Convert Array to Comma Separated String
Recommended:-JavaScript Convert JSON String to JSON Object