How to Convert a JavaScript Object to JSON (and the Gotchas That Break It)
Someone hands you a chunk of JavaScript, you paste it into a JSON field, and it explodes. Unquoted keys, single quotes, a trailing comma. It looks like JSON, it is shaped like JSON, but a strict parser rejects it. This is the most common "why won't this work" I see.
A JavaScript object and JSON are close cousins, not twins. Here is exactly how they differ, the fastest way to convert one to the other, and the JSON.stringify traps that will quietly throw away your data if you do it in code.
A JS object is not JSON
Here is a perfectly valid JavaScript object that is invalid JSON:
const user = {
name: 'Ada', // unquoted key, single quotes
roles: ['admin',], // trailing comma
active: true,
}The JSON version has to be stricter:
{
"name": "Ada",
"roles": ["admin"],
"active": true
}The rules JSON enforces that JavaScript does not:
- Keys must be double-quoted strings. No unquoted keys.
- Double quotes only, never single.
- No trailing commas.
- No comments, no functions, no
undefined.
To convert without hand-fixing all that, paste it into the JS to JSON converter. It quotes the keys, swaps the quotes, strips trailing commas, and hands back valid JSON. It runs in your browser, so the object is not uploaded anywhere.
Doing it in code: JSON.stringify and its traps
In JavaScript itself, JSON.stringify(obj) is the built-in way. It works, but it silently changes or drops several things, and that is where bugs hide:
JSON.stringify({
a: undefined, // dropped entirely
b: () => 1, // functions dropped
c: new Date(), // becomes an ISO string "2026-06-30T..."
d: NaN, // becomes null
e: 10n, // BigInt throws a TypeError
})
// -> '{"c":"2026-06-30T..."}' (a, b, d handled surprisingly)undefinedand functions are removed, so keys quietly vanish.Dateobjects become strings. They do not round-trip back to Dates on parse.NaNandInfinitybecomenull.BigIntthrows a TypeError. You have to convert it to a string first.- A circular reference throws "Converting circular structure to JSON".
The pretty-printed version, for logs or files, is JSON.stringify(obj, null, 2)(the 2 is the indent). That third argument is the one everyone forgets.
When to use the tool vs code
If you have a JS object sitting in front of you (a config snippet, something a colleague pasted, a fixture) and you just need clean JSON, the converter is faster than writing throwaway code. If you are producing JSON inside a running program, useJSON.stringify and keep the traps above in mind. Either way, once you have the JSON, the JSON viewer is handy for eyeballing the structure and confirming it parses.
FAQ
Why is my JavaScript object not valid JSON?
Usually unquoted keys, single quotes, or a trailing comma. JSON requires double-quoted keys and values and forbids trailing commas, comments, and functions.
How do I convert a JS object to JSON?
In code, JSON.stringify(obj). For a snippet you have on hand, paste it into the JS to JSON converter, which fixes the syntax for you in the browser.
Why did some keys disappear after stringify?
JSON.stringify drops undefined values and functions. If a key vanished, its value was one of those.
Is the converter private?
Yes. It runs in your browser, so your object is never uploaded to a server.
Convert yours now with the free JS to JSON converter, then check the result in the JSON viewer.