Convert JSON to a JavaScript object

How JSON text becomes a JavaScript object with JSON.parse, what changes and what doesn't, where precision and types are lost, and when to keep plain JSON.

Turning JSON into a JavaScript object usually means one of two things. You may want to parse a JSON string at runtime into a live object your code can use. Or you may want to rewrite JSON text as a JavaScript object literal to paste into source code. These are different tasks with different risks. This review covers what each one changes, where meaning can be lost, and when it is better to keep plain JSON text.

Is converting JSON to a JavaScript object the right step?

If your code needs to read or change the data, convert it. Parsing gives you real properties, arrays and values to work with instead of a string you would otherwise have to search by hand. The built-in JSON.parse method is the standard way to do this. It needs no library or online tool.

The main tradeoff is that JSON has only a few value types: strings, numbers, booleans, null, arrays and objects. Parsing does not turn a date string into a Date. It does not restore undefined or functions. It also cannot keep integers larger than JavaScript can represent exactly. If the data relies on any of these, a plain parse will quietly give you something slightly different from what the author meant.

Keep the data as JSON text in these cases:

  • You are storing it in a file, database field or cache.
  • You are sending it over a network or to another process.
  • The consumer is written in another language.
  • The data is configuration that people edit and that tools validate against JSON rules.
  • You only need to check whether it is valid, not use its contents.

What parsing actually changes

JSON.parse reads a string and builds new objects, arrays and primitive values in memory. The input string is not modified. The result is a separate structure that no longer has any whitespace, indentation or key quoting. Those exist only in text.

const text = '{"name": "Ada", "tags": ["math", "code"], "active": true}';
const data = JSON.parse(text);

console.log(data.name);      // "Ada"
console.log(data.tags[1]);   // "code"
console.log(typeof data);    // "object"

Invalid input does not return a partial result. JSON.parse throws a SyntaxError, so wrap the call in try/catch whenever the text comes from outside your code:

function safeParse(text) {
  try {
    return { ok: true, value: JSON.parse(text) };
  } catch (err) {
    return { ok: false, error: err.message };
  }
}

In browsers and other environments with the Fetch API, response.json() reads the response body and parses it as JSON in one step. The result is the same kind of object, and the same type limits apply.

"String to JSON" usually means the reverse direction

Searches for "string to JSON in JavaScript" use the terms loosely. JSON is itself a string format. So the method that turns a string into an object is JSON.parse. JSON.stringify goes the other way: it turns an object into JSON text. If you have text and want properties, you need parse. If you have an object and want something to save or send, you need stringify.

Where values do not survive unchanged

  • Dates: a value like "2026-10-31T00:00:00Z" stays a string. Convert it yourself, or use a reviver function.
  • Large integers: numbers above Number.MAX_SAFE_INTEGER (2^53 − 1) can be rounded to the nearest representable value without any error. IDs from some APIs are sent as strings for this reason. Keep them as strings.
  • Duplicate keys: JSON.parse does not reject them. The last value for a key wins and earlier ones are dropped.
  • The "proto" key: JSON.parse creates an ordinary own property with that name. The same text pasted as an object literal in source code sets the object's prototype instead.
  • Round trips: JSON.stringify drops undefined values and functions and turns Dates into strings. Parsing that text again will not bring them back.

A reviver is a function passed as the second argument to JSON.parse. It sees every key and value and can replace a value, which lets you restore richer types on purpose:

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

Converting JSON text into an object literal

Some tools for converting JSON to a JS object online produce a JavaScript object literal instead of a parsed value. They typically remove quotes from keys and may switch to single quotes. This is a formatting change. Apart from the "proto" case above, the resulting object has the same values. Keys that are not valid identifiers, such as "first-name" or "2fa", must stay quoted. A converter that removes those quotes produces broken code.

The conversion only works cleanly in one direction. Valid JSON is valid JavaScript expression syntax in current engines. The reverse is not true: object literals can contain trailing commas, comments, single-quoted strings, undefined, NaN and functions, and none of these are valid JSON. Pasting an object literal into something that expects JSON will usually fail validation.

Avoid eval and new Function for turning text into an object. They run arbitrary code, while JSON.parse accepts only data.

Will the result stay readable and usable after copying or sharing?

A parsed object exists only in memory, so you cannot copy or share the object itself. What you can copy is a text representation of it, and which representation you copy matters.

  • Console output shows the object for inspection, not serialized data. Depending on the environment it may collapse nested values, show class names, or use a format that is not valid JSON. It is not a reliable source for copying data.
  • JSON.stringify(data, null, 2) produces indented, valid JSON that can be pasted elsewhere and parsed again.
  • An object literal pasted into source code works only in JavaScript. Other tools and languages will not accept it as JSON.

Indentation and line breaks make nested data much easier to scan and to compare in version control. They do not change the data, so pretty-printing for people and minifying for transfer are both safe. Reading order follows the order of keys in the text. In parsed objects, keys that look like integers are ordered numerically ahead of other keys. If key order matters for display, use an array.

Accessibility depends mostly on where the text appears, not on the conversion. Code shown as preformatted plain text keeps its characters for copying and for assistive technology. Code shown as an image, or styled with decorative characters, does not. No test results were available for how particular screen readers handle deeply nested JSON, so check with your own audience if this matters.

Using online converters

Pasting JSON into a web tool sends or exposes that text to software you do not control. No evidence was available about how any particular converter handles or keeps pasted data. A tool's appearance or claims are not enough to judge this. For API keys, tokens, personal records or anything confidential, parse locally instead: the browser developer console, Node.js or your editor can all run JSON.parse and JSON.stringify without the data leaving your machine.

Summary

Use JSON.parse, with error handling, whenever your code needs to work with JSON data. Watch for large numbers, dates and duplicate keys, because parsing does not preserve those as the author may have intended. Treat object-literal conversion as a code-formatting step. Keep the data as JSON text whenever it will be stored, sent or read by anything other than JavaScript. This review is based on documented language behavior. It does not include tests of specific tools or environments.