JSON Stringify

Next ❯JSON PHP

Used to convert an object into JSON string

  • Well, When you have to send an object to the server, it must be in string
    So to convert javaScript object into JSON string we require JSON.stringify()
  • Here, Object can have only JSON data types, otherwise it will skip keys having value other than JSON data types
  • Remember! You can also convert an array via this method into a string and can send it to the server

Syntax

JSON.stringify(object)

Example

var obj = {"name":"Riya","age":23};  // Object
Convert into a String

Before : [object Object]
After Stringify : {"name":"Riya","age":23}

var obj = {"name":"Riya","age":23};
var jsonString = JSON.stringify(obj);
$("p").html("Before : "+obj);
$("p").append("<br>After Stringify : "+ jsonString);
JSON.stringifysubject
JSON.stringifyclose
<!DOCTYPE html>
<html>
<head>
<title>Full Code</title>
</head>
<body>
<p style="text-align:center;"></p>
<script>
var obj = {"name":"Riya","age":23};
var jsonString = JSON.stringify(obj);
$("p").html("Before : "+obj);
$("p").append("<br>After Stringify : "+ jsonString);
</script>
</body>
</html>

  • JSON PHP
❮ Prev Arrays
Next ❯JSON PHP
TryOut Examples"Learn to Explore..!"

TryOut Editor

receipt