Use JSON.parse() to create JSON object from string in JavaScript

Description

The following code shows how to use JSON.parse() to create JSON object from string.

Example


<!--from   w  ww . jav  a  2s. c  om-->
<!DOCTYPE html>
<html>
<head>
<title>Operator Example</title>
<script type="text/javascript">
var book = {
title: "JavaScript",
authors: ["J" ],
edition: 3,
year: 2011
};

var jsonText = JSON.stringify(book);
document.writeln(jsonText);
var bookCopy = JSON.parse(jsonText);
document.writeln(bookCopy);
</script>

</head>
<body>

</body>
</html>

Click to view the demo

The code above generates the following result.

Use JSON.parse() to create JSON object from string in JavaScript
Home »
  Javascript Tutorial »
    Data Type »
      JSON
Javascript Tutorial JSON
Add toJSON() method to object and have it r...
Control the string indention converting JSO...
Convert JSON object to String in JavaScript
Convert selected fields from JSON to String...
Convert string to date when converting stri...
Handle field one by one during JSON to stri...
Set indentation character during JSON seria...
Use JSON.parse() to create JSON object from...