Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

JSON.parse

Parses a JSON string and returns the value.

Available since: ES5

Syntax

JSON.parse(text)

Parameters

ParameterTypeDescription
textstringA string in JSON format.

Return value

anyThe object, array or primitive value written in the string.

Examples

A string becomes an object:

const user = JSON.parse('{"name": "Anna", "age": 25}');
 
console.log(user.name);

Output:

Anna

An invalid string throws an error that must be caught:

try {
  JSON.parse('not JSON');
} catch (error) {
  console.log(error.name);
}

Output:

SyntaxError

Notes

In JSON, property names and strings use double quotes only. Always parse user-supplied and network data inside try...catch.

See also

  • JSON.stringify — Converts a value to a JSON string.
  • SyntaxError — A syntax error — in code, in JSON or in a regular expression.
  • try...catch — Catches errors so the program does not stop.

Related recipes

In the tutorial

  • JSON — The JSON format in JavaScript: turning objects into a string with JSON.stringify and back with JSON.parse.

Thanks for reading!

Donate to the site — we work hard for you.