.parse()
Published Jun 2, 2022
Contribute to Docs
The .parse()
method returns a new value taken from a provided JSON string.
Syntax
JSON.parse(jsonString, transformer);
The jsonString
must contain valid JSON. Otherwise, a SyntaxError
is thrown. Common JSON includes single objects or arrays of them. However, data types like booleans and numbers are also valid.
The transformer
function is an optional parameter that operates directly on the parsed jsonString
before the resulting transformation is returned.
Example
In the example below, a valid JSON object is stored in a variable, myJSONString
, and then passed into the .parse()
method as an argument:
let myJSONString = '{"Hello": "World"}';console.log(JSON.parse(myJSONString));
The output will look like this:
{ Hello: 'World' }
Codebyte Example
The following example further demonstrates how the .parse()
method works:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Full-Stack Engineer
A full-stack engineer can get a project done from start to finish, back-end to front-end.Includes 51 CoursesWith Professional CertificationBeginner Friendly150 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours