JSON to JavaScript Object Converter

Strict JSON can be converted to a JavaScript object with JSON.parse. JavaScript object literals need cleanup before they are valid JSON.

Convert JSON in JavaScript

If the input is already valid RFC 8259 JSON, the safest converter is JSON.parse(jsonText). It returns a normal JavaScript object, array, string, number, boolean, or null — and crucially, it does not evaluate code.

JSON Input

{
  "name": "Ada",
  "active": true,
  "skills": ["math", "notes"]
}

JavaScript Usage

const user = JSON.parse(jsonText);
console.log(user.name);      // "Ada"
console.log(user.active);    // true
console.log(user.skills[0]); // "math"

What Each JSON Value Becomes

The mapping is deterministic — every JSON value has exactly one JavaScript counterpart after JSON.parse:

JSON value JavaScript value after JSON.parse
{ … } plain object (Object.prototype)
[ … ] array (Array.prototype)
"hello" string
42, 3.14, -1e3 number
true, false boolean
null null
(nothing) undefined — never the result of JSON.parse, since JSON has no undefined literal

Object Literal Input Is Different

A JavaScript object literal such as { name: 'Ada', active: true } is not JSON. Before JSON.parse will accept it, you have to repair the differences: quote the keys, swap single quotes for double quotes, remove comments, and remove trailing commas. See JSON vs JavaScript object literal for the full side-by-side comparison, or paste the snippet into JSON Fix to apply all the fixes in one pass.

When to Avoid eval

Do not use eval to convert unknown text into an object. eval executes code — a malicious payload can run anything in the calling scope. JSON.parse only parses data, with a small fixed grammar; it can produce a value but cannot run a function. That's why strict JSON is the right shape for API payloads, configuration files, log lines, and pasted examples.

Edge Cases: Dates, undefined, and Functions

JSON has no Date, undefined, or function type. Dates round-trip as ISO strings, undefined values are dropped during JSON.stringify, and functions cannot be represented at all. The JSON vs JavaScript guide's round-trip section enumerates every type that gets lost or transformed.

If a JavaScript object contains a Date that you need back as a Date (not a string) after JSON.parse, pass a reviver function:

const user = JSON.parse(jsonText, (key, value) =>
  key === 'createdAt' ? new Date(value) : value
);

The reviver runs for every key/value pair and can return a replacement value — useful for restoring types that JSON couldn't carry natively.

TypeScript and Inferred Types

JSON.parse returns any because the parser cannot know the shape ahead of time. For typed access:

  • Validate at the boundary with a schema library like Zod or io-ts — the parsed type comes from the schema, so the type system reflects what was actually validated, not what was promised in a comment.
  • Generate an interface from a sample using the JSON to TypeScript tool — useful when iterating against an external API whose payloads you don't fully control.

A bare JSON.parse(text) as User cast compiles but doesn't verify anything at runtime; it's a promise to the compiler, not a guarantee.

The Reverse Direction: Object to JSON

JSON.stringify(value, null, 2) produces formatted JSON from a JavaScript value (the third argument 2 is the indent width). It silently drops undefined, functions, and Symbol keys; circular references throw TypeError. Pass a replacer function as the second argument to control which fields survive — useful for stripping internal fields, redacting secrets, or normalizing date formats before sending an object over the wire.

For a hands-on view of what JSON.stringify produces with different options, use the JSON Stringify tool — it shows the output for each indent and replacer choice without writing code.

See also

This guide pairs with JSON vs JavaScript object literal (the syntax-difference reference) and the repair workflow hub (for cleaning JS-style snippets back into valid JSON before parsing). For LLM-produced almost-JSON, see the LLM JSON repair hub.

Sources

  • RFC 8259 — the JSON Data Interchange Format (IETF, the canonical JSON grammar)
  • MDN — JSON.parse (the safe parser, including reviver behavior)
  • MDN — JSON.stringify (the reverse direction, including replacer behavior)
  • MDN — Object initializer (the JS object-literal syntax, ECMA-262 §13.2.5)

Last reviewed June 2026.