Skip to main content

js-to-json

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​

  1. Paste JS object literal β€” Anything that's a valid JS object expression. Wrapped in \{...\} or pasted from a const x = \{...\}.
  2. Convert β€” Tool parses with a permissive JS parser, then re-serialises as strict JSON.
  3. Format options β€” Pretty-print (2/4-space indent) or minified.
  4. Validate β€” Output verified as parseable by JSON.parse(); errors flagged.
  5. 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 js-to-json runs in your browser at https://ztools.zaions.com/js-to-json β€” no signup, no upload, no data leaves your device.

Open the tool β†—


Last updated: 2026-05-06 Β· Author: Ahsan Mahmood Β· Edit this page on GitHub