JavaScript Object to JSON Converter

Convert a JavaScript object literal, with all the syntax JSON forbids, into valid JSON.

JavaScript object
JSON
Runs locally in your browser

About the JavaScript Object to JSON Converter

Configuration copied from a JavaScript file is not JSON: keys are unquoted, strings use single quotes, comments and trailing commas are present, and values may be expressions. This converter evaluates the literal as JavaScript in an isolated function scope and serialises the resulting value with JSON.stringify, so all of that syntax is handled the way the language defines it rather than by pattern matching.

A leading const name =, export default or trailing semicolon is stripped automatically. Functions in the object are converted to their source text as strings so they are visible rather than silently dropped; undefined values are omitted, as JSON.stringify does. Because evaluation happens in a function with no access to the page, references to outside variables are errors rather than security problems.

For input that is nearly JSON but not quite, and where you would rather not evaluate anything, the JSON Fixer repairs the text instead.

How to use

  1. Paste the object literal, with or without its declaration.
  2. Copy the JSON on the right.
  3. Format or validate it further with the JSON tools if needed.

Common questions

Is the code executed?
The literal is evaluated in an isolated function so that JavaScript syntax is handled correctly. Do not paste code from untrusted sources with side effects; use the JSON Fixer for that.
What happens to Date objects and Maps?
Dates become ISO strings; Maps and Sets become empty objects, as JSON.stringify defines. Convert them to arrays first.
Why did it fail with "x is not defined"?
The object references a variable that does not exist in the snippet. Replace it with a literal value.