A JavaScript object literal is not JSON. To convert JavaScript object literal to JSON, quote every key, switch every string to double quotes, delete comments, delete trailing commas, then paste the result into the JSON Validator. Format only after the validator reports a valid value. Minify only if you need a compact body.
Do not ask a formatter to “fix” the literal. Format and minify both require a successful parse. A pretty-printed invalid string is still invalid.
Application code often looks like this:
{
// feature flags
id: 12,
name: 'Ada',
roles: ['admin', 'developer'],
active: true,
}
That is legal JavaScript in many files. It is not JSON. JSON requires double-quoted keys, double-quoted strings, no comments, and no comma after the last property or array item. undefined, functions, and NaN also have no JSON form. If you see those, you are not converting a literal — you are redesigning the payload.
People search “convert javascript object to json” and then paste the literal into a formatter. The first unexpected token is usually an unquoted key, a single quote, or a / from a comment. That error is correct. The tool did not fail. The input is a different language.
const payload =, TypeScript types, and trailing ;.// line comment and every /* */ block. JSON has no comment syntax, including JSONC files you might have open in the editor.id becomes "id". Nested objects get the same treatment.undefined with null only if the API treats missing and null the same. Otherwise remove the key. Do not leave undefined in the string.Keep this order. Quoting keys after you have already deleted commas is fine. Validating before you have finished quotes wastes time: you will get a new error on every pass and think the validator is flaky.
When the validator reports a valid object or array, paste the same string into the JSON Formatter if you need to read nesting. Use minify only when the payload is valid and you need a one-line body. Those two actions are the last steps in how to format, validate and minify JSON, not a substitute for conversion.
If validate fails at position 0, you still have a prefix: a notice, a BOM, or the word undefined from a console.log. Strip that, then convert again. If it fails on a comma, you missed a trailing comma. If it fails on a quote, you still have a single-quoted string or an unescaped inner quote.
Start with the literal above. After the token pass you should have:
{
"id": 12,
"name": "Ada",
"roles": ["admin", "developer"],
"active": true
}
Validate: valid JSON object. Format if you want the same structure with consistent indent. Minify if you will send it as a request body:
{"id":12,"name":"Ada","roles":["admin","developer"],"active":true}
If you left the comment or the trailing comma, validate fails and you do not format. That is the whole point of converting first.
A second common case is a PHP or Magento log that printed a JavaScript-looking dump with unquoted keys. Treat it as a literal, not as “almost JSON.” Run the same seven steps. If the dump includes object class names or extra parentheses, you are reading print_r or a custom inspector, not an object literal. Rebuild the payload from the fields you need instead of trying to regex the dump into JSON.
TypeScript object literals can include type assertions (as const) and satisfies clauses. Delete those before you quote keys. JSONC (JSON with comments) is what many editors use for config. Comments still have to go before a strict parser will accept the file. VS Code may display JSONC as valid in the editor and still fail JSON.parse in the browser. Trust the validator, not the syntax highlighter.
If the literal uses computed keys ([name]: value) or spread (...extra), you cannot convert by editing quotes. Those are runtime JavaScript. Evaluate the object in a scratch file, then JSON.stringify the result, or rebuild the object by hand from known keys.
Do not paste production secrets into a tab you will leave open. Conversion does not require a server on YallaSolve. The payload still sits in that tab until you clear it.
When you hand the result to someone else, say that it is JSON, not “the object from checkout.js.” Those are different artifacts. This guide exists so a team stops treating object literals and JSON as interchangeable in tickets.
Unquoted keys, single quotes, comments, and trailing commas are legal in many JS files and illegal in JSON. Convert those tokens, then validate.
No. Format runs after a successful parse. Use the validator to see the first illegal token, fix it, then format.
JSON has no form for either. Remove the key or replace undefined with null only if the API treats them the same. Do not leave the JS token in the string.
If you still have a live object, JSON.stringify is faster. This guide is for text you copied — a literal in a file or a dump — not a value already in memory.
Yes for the tokens. Delete type assertions and comments first, then quote keys and switch to double quotes. Computed keys and spreads cannot be converted by editing quotes.