How to Convert JavaScript Object Literal to JSON

Turn a JS object literal into legal JSON: quote keys, use double quotes, drop comments and trailing commas, then validate before you format or minify.

Last updated: October 2, 2026

Quick answer

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.

How a literal differs from JSON

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.

Convert the tokens in order

  1. Copy only the object or array. Strip const payload =, TypeScript types, and trailing ;.
  2. Remove every // line comment and every /* */ block. JSON has no comment syntax, including JSONC files you might have open in the editor.
  3. Quote every key: id becomes "id". Nested objects get the same treatment.
  4. Replace single quotes with double quotes. Escape any double quote that already sits inside a string.
  5. Delete the comma after the last property in each object and the last item in each array.
  6. Replace undefined with null only if the API treats missing and null the same. Otherwise remove the key. Do not leave undefined in the string.
  7. Paste the result into the validator. Read the first error. Fix that site. Validate again.

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.

Validate before you format

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.

Worked example

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.

When the source is TypeScript or JSONC

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.

On this page

Related Articles

Related solutions

Need a tool for this ?

Open the free tools — no signup.

FAQs

newsletter signup

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Innovative Solutions For Modern Needs
Copyright © 2026 Yallasolve. all rights reserved.