JavaScript .parse()
Published Jun 2, 2022
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:
Learn JavaScript on Codecademy
- A full-stack engineer can get a project done from start to finish, back-end to front-end.
- Includes 51 Courses
- With Professional Certification
- Beginner Friendly.150 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours