How to use JS Object to JSON
- Paste your object into JavaScript object literal. Arrays and single values work too.
- Choose an Indent: 2 spaces, 4 spaces, Tabs, or Minified for one line.
- Copy the JSON result or download it as
data.json. - 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
0x1F4to decimal - drops
undefinedproperties and turnsNaNandInfinityintonull
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?
What happens to undefined, NaN and Infinity?
Can I paste the whole const config = { … } line?
What if a key appears twice?
Why did my long ID change?
How do we know nothing is uploaded? Test it yourself on the privacy proof page.