JSON.parse()

Converts a string in JSON format into a JavaScript value, such as an object or array.

Returns
The JavaScript value corresponding to the JSON text.
On this page

Example

#
JavaScript
const json = '{"name":"Jack","level":3,"skills":["HTML","CSS"],"premium":true}'
const user = JSON.parse(json)

console.log(user.name)
console.log(user.skills[1])
console.log(user)
Console
Jack
CSS
{ name: 'Jack', level: 3, skills: ['HTML', 'CSS'], premium: true }

Definition and usage

#

The JSON.parse() method reads text in JSON format and creates the corresponding value from it: an object, an array, a string, a number, true, false, or null. It’s used for data from a server, from localStorage, or from configuration files.

The JSON format is stricter than object notation in JavaScript. Keys and strings must be in double quotes, there can be no comma after the last element, and comments aren’t allowed. Invalid text causes a SyntaxError, so parse data from an untrusted source inside a try...catch block.

The optional second argument (reviver) is a function called for every key-value pair. It lets you transform the result while it’s being read, for example turning date strings back into Date objects.

Syntax

#
Syntax
JSON.parse(text)
JSON.parse(text, reviver)

Parameters

#
  • text

    string

    Text in JSON format.
  • reviver

    optional function (key, value)

    A function that transforms every value as it’s read. Whatever it returns ends up in the result.

More examples

#
Safely parsing untrusted data
JavaScript
function safeParse(text, fallback) {
  try {
    return JSON.parse(text)
  } catch {
    return fallback
  }
}

console.log(safeParse('{"theme":"dark"}', {}))
console.log(safeParse("{theme: 'dark'}", {}))
Console
{ theme: 'dark' }
{}
Restoring dates with a reviver
JavaScript
const json = '{"title":"Exam","date":"2026-09-28T10:30:00.000Z"}'

const exam = JSON.parse(json, (key, value) => (key === 'date' ? new Date(value) : value))

console.log(exam.date instanceof Date)
console.log(exam.date.getUTCHours())
Console
true
10
Common syntax mistakes
JavaScript
const attempts = ["{'level': 1}", '{"level": 1,}', '{"level": 1}']

for (const text of attempts) {
  try {
    console.log(JSON.parse(text))
  } catch (error) {
    console.log(error.name)
  }
}
Console
SyntaxError
SyntaxError
{ level: 1 }

The first string uses single quotes, and the second has a comma after the last element. Both are valid in JavaScript, but not in JSON.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Always parse data from the network, from the user, or from localStorage inside try...catch. A single bad character throws a SyntaxError.
  • JSON.parse() won’t restore dates, Maps, or class instances. You can restore dates with a reviver function.
  • With fetch(), instead of JSON.parse(await response.text()), just use await response.json().

Related entries

#

Spotted a mistake or missing an example? Write to us.