YouTip LogoYouTip

Javascript Json Stringify

# JavaScript JSON.stringify() [![Image 3: JavaScript JSON](#) JavaScript JSON](#) * * * The JSON.stringify() method is used to convert a JavaScript value to a JSON string. ### Syntax JSON.stringify(value[, replacer[, space]]) **Parameter Description:** * **value:** Required. The JavaScript value to convert (usually an object or array). * **replacer:** Optional. A function or array used to transform the result. If the replacer is a function, JSON.stringify will call this function, passing in the key and value of each member. The return value is used instead of the original value. If this function returns undefined, the member is excluded. The key of the root object is an empty string: "". If the replacer is an array, only members with keys present in the array are converted. The conversion order of members is the same as the order of keys in the array. * **space:** Optional. Adds indentation, spaces, and line breaks to the text. If space is a number, the returned text is indented by the specified number of spaces at each level. If space is greater than 10, the text is indented by 10 spaces. space can also be a non-numeric value, such as: t. ### Return Value: Returns a string containing the JSON text. ### Example ### Example var str = {"name":"", "site":""}str_pretty1 = JSON.stringify(str)document.write("Only one parameter case:"); document.write("
"); document.write("
" + str_pretty1 + "
"); document.write("
"); str_pretty2 = JSON.stringify(str, null, 4)// Use four spaces for indentation document.write("Using parameters case:"); document.write("
"); document.write("
" + str_pretty2 + "
"); // pre is used for formatted output [Try it Β»](#) [![Image 4: JavaScript JSON](#) JavaScript JSON](#)
← File SeekJavascript Json Stringify β†’