JavaScript object literal to JSON converter

Paste a JavaScript object literal, even with its const declaration, and get strict JSON back with quoted keys, double quotes and no comments or trailing commas.

Live demo · real result from this tool

JSON

{  "name": "my-app",  "port": 8080,  "debug": false,  "hosts": [    "a.example.com",    "b.example.com"  ],  "retry": {    "attempts": 3,    "delayMs": 500  }}

Changes made: Removed the variable declaration around the value; Quoted unquoted keys (8×); Replaced single quotes with double quotes (3×); Removed comments; Removed trailing commas (2×); Converted hex, octal or binary numbers to decimal; Dropped undefined values.

Processed on your device — nothing is uploaded.

How to use JS Object to JSON

  1. Paste your object into JavaScript object literal. Arrays and single values work too.
  2. Choose an Indent: 2 spaces, 4 spaces, Tabs, or Minified for one line.
  3. Copy the JSON result or download it as data.json.
  4. If the input contains code rather than data, read the error line and the frame under it, replace that part with a literal value, and try again.

What it does and when to use it

Config objects, test fixtures and mock data often start life as JavaScript, with unquoted keys, single quotes and comments. APIs, package.json-style files and most other languages want strict JSON instead. This tool does the conversion without the risk of running the code through eval.

It goes the other way with JSON to JS object. Check the result with the JSON validator, or tidy and sort it with the JSON formatter. If you have broken JSON rather than a JavaScript literal, JSON repair reports each fix it makes.

How it works

A tolerant parser reads the text token by token and builds the value. Along the way it:

  • quotes bare keys and converts single-quoted and plain backtick strings to double quotes
  • removes // and /* */ comments and trailing commas
  • converts hex, octal and binary numbers such as 0x1F4 to decimal
  • drops undefined properties and turns NaN and Infinity into null

The result is printed as standard JSON as defined in RFC 8259. Functions, methods, template literals with ${…} and other expressions are rejected with a position rather than guessed at.

Worked examples

A config object. This input, including the declaration and comment:

const config = {
  name: 'my-app',
  port: 8080, // default port
  debug: false,
  hosts: ['a.example.com', 'b.example.com',],
  retry: { attempts: 3, delayMs: 0x1F4 },
  onError: undefined,
};

comes out with 2-space indent as JSON whose retry block reads "attempts": 3 and "delayMs": 500. The comment, the trailing comma and onError are gone.

Values JSON cannot hold. { id: 12345678901234567890n, ratio: NaN } with Minified gives {"id":12345678901234567000,"ratio":null}. The BigInt lost its last digits and NaN became null; the note under the result lists the changes, including the rounding, so check output like this before you rely on it.

An expression. { when: new Date() } stops with “line 1, column 9: Expressions cannot be represented in JSON.” Replace it with a date string such as "2026-10-07T00:00:00Z".

Limits and tips

  • Literals only. Spread (...defaults), computed keys, variables and getters cannot be converted.
  • Clean-up is listed. The parser also fills in some missing commas, so { a: 1 b: 2 } converts, and a note under the result names every change it made. Compare the output with your intent.
  • Large numbers above 2^53 − 1 lose precision; keep them as strings.
  • Key order is kept as written. Use the JSON sorter if you want keys in order.

Frequently asked questions

Does the tool run my JavaScript?
No. It reads the text with its own parser and only accepts literal values. Anything that would need running, such as a function call, a variable name or new Date(), is reported as an error with its line and column.
What happens to undefined, NaN and Infinity?
Properties set to undefined are dropped, the same as JSON.stringify does. NaN and Infinity become null, because JSON has no way to write them.
Can I paste the whole const config = { … } line?
Yes. A leading const, let or var declaration, export default or module.exports = is removed before parsing, and the final semicolon is ignored.
What if a key appears twice?
The last value wins: { a: 1, a: 2 } becomes {"a":2}. RFC 8259 says object names should be unique, so it is worth fixing the source.
Why did my long ID change?
Numbers are stored as JavaScript numbers. Integers beyond 2^53 − 1, including BigInt literals such as 12345678901234567890n, are rounded, and the list of changes under the result says so. Put such values in quotes so they stay strings.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close