JavaScript JSON parse()

Back to JS page

Table of Contents

The JSON.parse() Method

When receiving data from a web server, the data is always a string.

Parse the data with JSON.parse(), and the data becomes a JavaScript object.

Parsing a JSON Object

JSON.parse() converts a JSON string into a JavaScript object:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript JSON parse()</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Parsing a JSON Object</h4>
<p id="demo"></p>

<script>
const jsonString = '{"name":"John","age":30,"city":"New York"}';
const obj = JSON.parse(jsonString);

document.getElementById("demo").innerHTML =
  "Name: " + obj.name + "<br>" +
  "Age: " + obj.age + "<br>" +
  "City: " + obj.city;
</script>

</body>
</html>

Example 1

Result View Example

Parsing a JSON Array

JSON.parse() can also parse a JSON array into a JavaScript array:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript JSON parse()</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Parsing a JSON Array</h4>
<p id="demo"></p>

<script>
const jsonArray = '["Ford","BMW","Audi","Fiat"]';
const cars = JSON.parse(jsonArray);

let text = "";
for (let car of cars) {
  text += car + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Example 2

Result View Example

Parsing Other JSON Values

JSON.parse() can parse all valid JSON value types:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript JSON parse()</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Parsing Other JSON Values</h4>
<p id="demo"></p>

<script>
const jsonString = '{"string":"Hello","number":42,"float":3.14,"boolean":true,"nullValue":null}';
const obj = JSON.parse(jsonString);

document.getElementById("demo").innerHTML =
  "String: " + obj.string + "<br>" +
  "Number: " + obj.number + "<br>" +
  "Float: " + obj.float + "<br>" +
  "Boolean: " + obj.boolean + "<br>" +
  "Null: " + obj.nullValue;
</script>

</body>
</html>

Example 3

Result View Example

The Reviver Function

The JSON.parse() method can accept a second parameter called a reviver function.

The reviver function can be used to transform the parsed values:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript JSON parse()</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>The Reviver Function</h4>
<p id="demo"></p>

<script>
const jsonString = '{"name":"John","birth":"1990-05-15","city":"New York"}';
const obj = JSON.parse(jsonString, function(key, value) {
  if (key === "birth") {
    return new Date(value);
  }
  return value;
});

document.getElementById("demo").innerHTML =
  "Name: " + obj.name + "<br>" +
  "Birth: " + obj.birth + "<br>" +
  "Birth (Date object): " + obj.birth.getFullYear();
</script>

</body>
</html>

Example 4

Result View Example

Handling Parse Errors

Invalid JSON will cause a parse error. Use try/catch to handle errors:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript JSON parse()</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Handling Parse Errors</h4>
<p id="demo1"></p>
<p id="demo2"></p>

<script>
// Valid JSON
const validJSON = '{"name":"John","age":30}';
try {
  const obj = JSON.parse(validJSON);
  document.getElementById("demo1").innerHTML = "Valid: " + obj.name;
} catch(err) {
  document.getElementById("demo1").innerHTML = "Error: " + err.message;
}

// Invalid JSON (single quotes)
const invalidJSON = "{'name':'John','age':30}";
try {
  const obj = JSON.parse(invalidJSON);
  document.getElementById("demo2").innerHTML = "Valid: " + obj.name;
} catch(err) {
  document.getElementById("demo2").innerHTML = "Invalid JSON error: " + err.message;
}
</script>

</body>
</html>

Example 5

Result View Example

Document

Document in project

You can Download PDF file.

Reference