.parse()

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:

Edit this page on GitHub

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your feedback form.

Learn JavaScript on Codecademy