HomeFormattersCodeJavaScript Object to JSON Formatter

JavaScript Object to JSON Formatter

Code

Convert a JavaScript object literal to valid JSON instantly. Handles unquoted keys, single quotes, trailing commas, and comments. In-browser — nothing uploaded.

What is a JS→JSON?

The JavaScript Object to JSON Formatter converts a JavaScript object literal — with unquoted keys, single-quoted strings, trailing commas, and comments — into valid, formatted JSON. It bridges the gap between JavaScript developer shorthand and the strict JSON format required by APIs, configuration files, and storage systems.

JavaScript object literal syntax is more permissive than JSON: keys don't need to be quoted, strings can use single quotes, trailing commas are allowed, and comments are supported. Developers often write configuration, test fixtures, or mock data using this relaxed JavaScript syntax, then need to convert it to JSON for deployment or API usage.

The formatter applies a series of transforms in sequence:

  1. Strip comments — removes // line comments and /* */ block comments
  2. Convert single to double quotes — replaces single-quoted strings with double-quoted JSON strings
  3. Quote bare keys — adds double-quotes around unquoted object keys
  4. Remove trailing commas — removes commas before } or ]
  5. Parse and reformat — validates the result with JSON.parse and outputs with JSON.stringify

What it cannot convert: function values, undefined, Symbol, regular expressions, new Date(), Infinity, NaN, or circular references — these have no JSON equivalent. The formatter returns a clear error message for these cases.

Related tools: JSON Formatter for formatting already-valid JSON, JSON to TypeScript Interface for generating TypeScript interfaces from JSON.

All processing is client-side. No data is transmitted.

How to use this JS→JSON calculator

  1. Paste the JavaScript object into the input textarea — any valid JS object literal syntax.
  2. Select the indent style — 2 spaces, 4 spaces, tab, or minified.
  3. Check the output panel — valid JSON appears instantly; errors show in the output with explanation.
  4. Copy the JSON output using the copy button.
  5. If conversion fails: check for functions, undefined, template literals, or new Date() — remove or replace these before converting.
  6. Related tools: use the JSON Formatter if the input is already valid JSON.

Formula & Methodology

Conversion steps applied in order:

1. Strip // line comments 2. Strip /* block comments */ 3. Convert 'single-quoted strings' → "double-quoted strings"    (handles escaped single quotes inside: \' → ') 4. Quote bare keys: { key: → { "key":    (regex: /([{,]\s*)([a-zA-Z_$][a-zA-Z0-9_$]*)(\s*:)/g) 5. Remove trailing commas: , } → } and , ] → ] 6. JSON.parse(result) → validates; catches any remaining syntax errors 7. JSON.stringify(parsed, null, indent) → canonical formatted output

Before and after example:

javascript // JavaScript object (input) {   name: 'Alice',  // user name   age: 30,   tags: ['admin', 'user'], } 

json {   "name": "Alice",   "age": 30,   "tags": ["admin", "user"] } 

Frequently Asked Questions

JavaScript object literals and JSON look similar but have key differences. JSON keys must be double-quoted strings; JavaScript object literals allow unquoted bare keys. JSON strings must use double quotes; JavaScript allows single quotes. JSON does not allow trailing commas; JavaScript objects do. JSON does not support comments; JavaScript supports // and /* */. JSON does not support undefined, functions, Dates, or symbols — these cannot be serialised to JSON.
You need to convert a JS object to JSON when sending data in an HTTP request body (APIs accept JSON, not raw JS syntax), storing configuration in a JSON file, persisting data to localStorage (which stores strings, not objects), using JSON.stringify in code, or sharing data with systems that accept only standard JSON. Developers often write configuration or test data as JS objects (with unquoted keys and single quotes) but need to convert them to valid JSON before use.
The formatter cannot convert JavaScript features that have no JSON equivalent: function values (functions cannot be serialised), undefined values (undefined is not a valid JSON value — JSON has only null), Symbol values, regular expressions (RegExp objects), Dates (a Date object is serialised as an ISO string, but a `new Date()` expression cannot be evaluated), and circular references. If the object contains any of these, the formatter shows an error message.
Yes — the formatter converts single-quoted string values to double-quoted JSON strings. For example, `name: 'Alice'` becomes `"name": "Alice"`. It also handles escaped single quotes inside single-quoted strings: `'it\'s'` becomes `"it's"`. However, template literals (backtick strings) are not supported.
Yes — trailing commas after the last property in an object or the last item in an array are removed. Trailing commas are valid JavaScript but invalid JSON. For example, `{ name: 'Alice', }` becomes `{"name": "Alice"}` in the JSON output.
Yes — both single-line comments (`// comment`) and block comments (`/* comment */`) are removed before conversion. This is useful when you have annotated JavaScript configuration objects that you need to convert to JSON for deployment.
No — all processing runs entirely in your browser. The JavaScript object is never sent to any server, stored, or logged. The tool works offline once loaded.
Paste your JavaScript object literal into the input textarea. The formatter strips comments, converts single quotes to double quotes, quotes bare keys, removes trailing commas, and then uses JSON.parse / JSON.stringify to produce valid, formatted JSON. Select the indent level (2 spaces, 4 spaces, tab, or minified) and copy the output.
Four indent options are available: 2 spaces (standard for most JavaScript/TypeScript projects), 4 spaces (common in Python and some enterprise codebases), tab (used by some style guides and accessibility preferences), and minified (no whitespace — compact single-line JSON for transmission or storage where size matters).
The [JSON Formatter](/json-formatter/) accepts valid JSON as input and reformats it (pretty-print, minify). It will reject JavaScript object syntax with unquoted keys or single quotes. The JS Object to JSON Formatter accepts JavaScript object literal syntax and converts it to valid JSON first, then formats it. Use this tool when the input is JavaScript code; use the JSON Formatter when the input is already valid JSON.
Yes — the formatter handles deeply nested objects and arrays by recursively processing the input through JSON.parse and JSON.stringify. The depth limit is determined by the JavaScript engine's stack depth, which is more than sufficient for any practical configuration or data object. Very large objects (hundreds of kilobytes of text) may take a moment to process but will not crash the browser.
Also known as
JS object to JSONJavaScript to JSON converterconvert object to JSONparse JS objectstringify JavaScript