JavaScript Object to JSON Formatter
CodeConvert 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:
- Strip comments — removes
//line comments and/* */block comments - Convert single to double quotes — replaces single-quoted strings with double-quoted JSON strings
- Quote bare keys — adds double-quotes around unquoted object keys
- Remove trailing commas — removes commas before
}or] - Parse and reformat — validates the result with
JSON.parseand outputs withJSON.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
- Paste the JavaScript object into the input textarea — any valid JS object literal syntax.
- Select the indent style — 2 spaces, 4 spaces, tab, or minified.
- Check the output panel — valid JSON appears instantly; errors show in the output with explanation.
- Copy the JSON output using the copy button.
- If conversion fails: check for functions,
undefined, template literals, ornew Date()— remove or replace these before converting. - 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 outputBefore 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