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.
Example
#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)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
#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
#function safeParse(text, fallback) {
try {
return JSON.parse(text)
} catch {
return fallback
}
}
console.log(safeParse('{"theme":"dark"}', {}))
console.log(safeParse("{theme: 'dark'}", {}))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())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)
}
}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
localStorageinsidetry...catch. A single bad character throws aSyntaxError. JSON.parse()won’t restore dates,Maps, or class instances. You can restore dates with areviverfunction.- With
fetch(), instead ofJSON.parse(await response.text()), just useawait response.json().
Related entries
#- JSON.stringify()Converts a JavaScript value, such as an object or array, into a string in JSON format.
- try...catchCatches errors thrown in the try block and handles them in catch. The optional finally always runs.
- fetch()Sends an HTTP request and returns a promise with the server’s response (a Response object).
- localStorageBrowser storage for key-value pairs that survives page refreshes and browser restarts.
- structuredClone()Creates a deep copy of a value, including nested objects, dates, Maps, and Sets.
Spotted a mistake or missing an example? Write to us.