JavaScript JSON.parse(): Parse JSON Strings, Objects, and Files

JSON.parse in JavaScript: valid JSON strings, SyntaxError on bad input, reviver functions, and safe parsing habits.

Published

Updated

Read time 2 min read

Reviewed byDeepak Prasad

JavaScript JSON.parse(): Parse JSON Strings, Objects, and Files

JavaScript JSON.parse() turns a JSON text string into a JavaScript value—objects, arrays, primitives, or null. It throws SyntaxError on invalid JSON, so production code almost always wraps parsing in try / catch error handling or validates input first. Remember that JSON.parse expects a string, not an object you already built in memory (double-parsing is a common mistake during refactors).

Environment: Node.js v20.18.2. After each runnable snippet, the following paragraph states the expected console output (order and values).


Method 1: Parse a JSON String

javascript
const json = '{"name":"Ana","age":30}';
const obj = JSON.parse(json);

console.log(obj.name);
Output

You should see one line logging Ana.

The JSON string becomes a normal JavaScript object.


Method 2: Handle Invalid JSON Safely

Invalid JSON throws a SyntaxError, so parse external data inside try...catch.

javascript
try {
  JSON.parse("{bad json}");
} catch (error) {
  console.log(error instanceof SyntaxError);
}
Output

You should see one line logging true.


Method 3: Use a Reviver Function

A reviver can transform values while parsing.

javascript
const data = JSON.parse('{"created":"2026-06-11"}', (key, value) => {
  return key === "created" ? new Date(value) : value;
});

console.log(data.created instanceof Date);
Output

You should see one line logging true.


Method 4: Parse JSON from a File or API

In a browser, load JSON with fetch() and parse the response body:

javascript
// In a browser you would use fetch("/data.json").
const data = JSON.parse(responseBody);
console.log("items:", data.items.length);
Output

In Node.js, you can read a JSON file as text and then parse it (this example avoids import/readFile so it runs in the in-browser sandbox):

javascript
const text = '{"name":"Ada","score":99}';
const data = JSON.parse(text);
console.log(data.name, data.score);
Output

Common Questions About Parsing JSON in JavaScript

How do I parse JSON in JavaScript?

Use JSON.parse(jsonString) to convert a JSON string into a JavaScript value.

Can JSON.parse parse a JavaScript object?

No. It parses strings. If you already have an object, do not pass it to JSON.parse().

How do I parse a JSON file in JavaScript?

In browsers, use fetch() and response.json(). In Node.js, read the file as text and pass it to JSON.parse().


Summary

JavaScript JSON.parse() converts JSON text into JavaScript objects, arrays, strings, numbers, booleans, or null. Use try...catch for untrusted JSON because invalid input throws a SyntaxError. Use a reviver when values need transformation during parsing, such as turning date strings into Date objects. For JSON files, load the file text first in Node.js or use response.json() in browser fetch code.


Official Documentation

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …