JavaScript to JSON Converter
A JS-to-JSON converter transforms JavaScript object literal syntax into strict JSON — quote keys with double quotes, replace single quotes with double quotes, remove trailing commas, strip comments, escape special characters. JS object literals are MORE lenient than JSON; JSON has strict rules. Pasted JS often comes from console output, code snippets, or hand-written config that won't parse as JSON. The ZTools JS to JSON Converter handles unquoted keys, single quotes, trailing commas, comments (// and /* */), undefined → null conversion, and emits standards-conformant JSON.
Use cases
Console output → API payload
Copied a JS object from console.log(). Need to send via curl as JSON. Converter cleans up the syntax.
Hand-written config to JSON
Wrote \{name: "x", value: 42, // important\} casually; need strict JSON for a tool that requires it.
Migration from JS config to JSON
Modernising a codebase: config.js exporting an object → config.json. Converter does the heavy lifting.
Test fixtures
JS tests use object literals; CI/external fixtures may want JSON. Quick conversion.
How it works
- Paste JS object literal — Anything that's a valid JS object expression. Wrapped in
\{...\}or pasted from aconst x = \{...\}. - Convert — Tool parses with a permissive JS parser, then re-serialises as strict JSON.
- Format options — Pretty-print (2/4-space indent) or minified.
- Validate — Output verified as parseable by JSON.parse(); errors flagged.
- Copy / download — .json file or clipboard.
Examples
Input: \{name: 'Alice', age: 30, // age in years\\n active: true,\}
Output: \{"name":"Alice","age":30,"active":true\} — keys quoted, comments removed, trailing comma stripped.
Input: \{ undefined: undefined, fn: () => \{\} \}
Output: WARNING: undefined and functions stripped (JSON has no representation). Output: \{\}.
Input: Complex nested object
Output: Pretty-printed JSON with 2-space indent.
Frequently asked questions
What gets stripped?
undefined (no JSON equivalent — becomes null or omitted), functions (no JSON equivalent), Symbols, and circular references. Tool warns when stripping.
Are comments preserved?
No — JSON has no comment spec. Tool strips both // and /* */ comments. For "JSON with comments", use JSONC (used by VS Code config) — different format.
Trailing commas?
Removed. JSON spec disallows. JS allows. Tool handles silently.
BigInt / Date / regex?
JSON doesn't support these natively. BigInt → string representation. Date → ISO string (recommended). Regex → /pattern/flags as string. Tool warns on conversion.
Can I round-trip JSON → JS → JSON?
Yes — both directions are loss-less for plain objects. With BigInt/Date/etc., expect lossy conversion.
JSON5 support?
JSON5 (relaxed JSON) supports comments, trailing commas, single quotes. Use a dedicated JSON5 parser; this tool emits strict JSON.
Tips
- Strict JSON: double quotes only, no comments, no trailing commas, no functions, no undefined.
- For configs that need comments, use JSONC (JSON with comments) or YAML, not JSON.
- Always pretty-print for human-edited JSON; minify for over-the-wire / size.
- Validate output by attempting
JSON.parse()— if it throws, the converter missed something. - For dates, use ISO 8601 strings — universally parseable across languages.
Try it now
The full JavaScript to JSON Converter runs in your browser at https://ztools.zaions.com/js-to-json — no signup, no upload, no data leaves your device.
Last updated: 2026-05-06 · Author: Ahsan Mahmood · Edit this page on GitHub