Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP W3.CSS C C++ C# HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

JS Tutorial

JS Home JS Introduction JS Where To JS Output JS Syntax JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Timers JS Objects JS Scope JS Dates JS Temporal  New JS Arrays JS Sets JS Maps JS Iterations JS Math JS RegExp JS Data Types JS Errors JS Debugging JS Style Guide JS Reference JS Projects  New JS Versions JS HTML DOM JS HTML Events JS HTML First

JS Advanced

JS Functions JS Objects JS Classes JS JSON JS Asynchronous JS Modules JS Meta & Proxy JS Typed Arrays JS DOM Navigation JS Browser API JS Web API JS Graphics

Old Technologies

JS AJAX JS jQuery JS JSONP

JS Examples

JS Examples

JavaScript JSON Parse

The JSON.parse() Method

The JSON.parse() method converts JSON text into a JavaScript value.

After parsing, the value can be used like any other JavaScript value.

JSON is text

JSON.parse() converts JSON text into a JavaScript value.

Syntax

JSON.parse(text, reviver)
Parameter Description
text The JSON text to parse.
reviver An optional function that transforms parsed values.

Parsing a JSON Object

Convert a JSON text into a JavaScript object.

Example

A JSON object becomes a JavaScript object.

const text = '{"name":"John","age":30,"city":"New York"}';

const person = JSON.parse(text);

let name= person.name;
Try it Yourself »

After parsing, object properties can be accessed with normal JavaScript syntax.


Parsing a JSON Array

Convert a JSON text into a JavaScript array.

Example

A JSON array becomes a JavaScript array.

const text = '["Ford","Volvo","BMW"]';

const cars = JSON.parse(text);

let name= cars[0];
Try it Yourself »

After parsing, you can use normal JavaScript array methods.


Parsing Other JSON Values

JSON.parse() can parse any valid JSON value.

JSON Text JavaScript Value
"John" String
42 Number
true Boolean
null null
[1,2,3] Array
{"x":1} Object

Example

Convert any JSON text into a JavaScript value.

let value;

value = JSON.parse('"John"');
value = JSON.parse('42');
value = JSON.parse('true');
value = JSON.parse('null');
Try it Yourself »


Invalid JSON

If the JSON text is not valid, JSON.parse() throws a SyntaxError.

Invalid JSON

const text = "{name:'John'}";

JSON.parse(text);

The example above is invalid because of 2 reasons:

Property names are not enclosed in double quotes.

Strings cannot use single quotes.

Valid

{"name":"John"}

Invalid

{'name':"John"}
{"name":'John'}
{name:"John"}
{"name":John}

Handling Parse Errors

Use try...catch to handle invalid JSON.

Example

const text = "{name:'John'}";

try {
  const person = JSON.parse(text);
}
catch(err) {
  myDisplayer(err);
}
Try it Yourself »

The Reviver Function

An optional reviver function can be called for every parsed property.

The return value replaces the original value.

Example

const text = '{"name":"John","age":"30"}';

const person = JSON.parse(text, function(key, value) {
  if (key == "age") {
    return Number(value);
  }
  return value;
});

document.getElementById("demo").innerHTML = typeof person.age;
Try it Yourself »

The example converts the string "30" into the number 30.


Common Parsing Mistakes


Parsing a JavaScript Object

JSON.parse() expects JSON text, not a JavaScript object.

Wrong

const person = {name: "John"};

const result = JSON.parse(person);

Correct

const text = '{"name":"John"}';

const person = JSON.parse(text);

Parsing JSON Twice

After JSON has been parsed, the result is already a JavaScript value.

Wrong

const person = JSON.parse('{"name":"John"}');

const result = JSON.parse(person);

Note

Call JSON.parse() only when the value is JSON text.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->