js-object-to-json
A JavaScript object to JSON converter takes a JavaScript object literal β the kind you'd type into a .js file with unquoted keys, single quotes, or trailing commas β and outputs strict JSON that parsers will accept by adding double quotes around keys, replacing single quotes with double quotes, removing trailing commas, and rejecting JS-only constructs like comments, undefined, functions, or NaN. The ZTools JS Object to JSON Converter handles arbitrarily nested objects, preserves number precision, supports template-literal-style multi-line strings (when valid as JSON strings), reports any JS-specific values that can't convert, and produces both compact and pretty-printed output.
Use casesβ
Pasting a JS object literal into a JSON config fileβ
You copied a const config = \{ ... \} from a JavaScript file. Convert it to valid JSON for a .json config β no manual quoting required.
Sharing JS data with non-JS toolsβ
Developer says "here's the data" and pastes a JS object. Convert to JSON to feed into Postman, jq, or any non-JS tool.
Building JSON test fixtures from JS examplesβ
Your test code uses inline objects for setup. To save them as separate .json fixtures, convert and dump to file.
Cleaning up "JSON" you pasted that isn't actually JSONβ
A teammate sends you a config they wrote, calling it JSON. It has trailing commas and unquoted keys (i.e., JS). Convert and validate.
How it worksβ
- Paste the JS object literal β Wrap in
\{ ... \}or[ ... ]. Trailing commas, unquoted keys, single quotes, comments β all common JS-isms accepted. - Parser tokenizes the JS β A small JS parser interprets the literal. Reports specific JS constructs that can't convert: undefined values, functions, NaN, Infinity, BigInt literals.
- Conversion to strict JSON β Keys quoted, single quotes β double quotes, trailing commas removed, comments stripped. Output validated against JSON.parse() before display.
- Read both compact and pretty β Two outputs: minified single-line JSON for storage, pretty-printed (2-space indent) for human reading.
Examplesβ
Input: {name: 'Alice', age: 30,}
Output: {"name":"Alice","age":30}
Input: {users:[{id:1, role:'admin'},{id:2, role:'user'}]}
Output: {"users":[{"id":1,"role":"admin"},{"id":2,"role":"user"}]}
Input: {date: new Date(), fn: () => {}}
Output: ERROR: cannot convert "new Date()" or function values β they have no JSON representation. Convert dates to ISO strings, replace functions with serializable data.
Frequently asked questionsβ
Why isn't my JavaScript object valid JSON?
Common reasons: 1) keys aren't quoted (name: instead of "name":). 2) Single quotes around strings ('Alice' instead of "Alice"). 3) Trailing commas (\{a:1,\}). 4) Comments. 5) JS-only values (undefined, NaN, functions). The converter handles 1-4 automatically; 5 needs manual replacement.
How does it handle dates?
JavaScript Date objects can't be serialized as JSON directly β they need to become ISO strings. The converter rejects new Date(...) and prompts you to replace with a literal string ISO date like "2026-05-05T00:00:00Z".
What if my object has methods or functions?
JSON has no function representation. The converter rejects function values; you need to remove them or replace with a placeholder string before converting.
Does it handle nested objects?
Yes β any depth. Each level is converted recursively, so nested JS object literals all become valid JSON.
How does it handle BigInt?
BigInt literals (123n) are not valid JSON β JSON only has Number. The converter flags BigInts; convert them to strings before serialization, or accept precision loss.
Tipsβ
- For dates, use ISO strings like "2026-05-05T12:34:56Z" β universally parseable, JSON-friendly.
- For BigInts (very large IDs), convert to strings before JSON serialization to preserve precision.
- When pasting JS code, include the wrapping braces β paste
\{...\}, notname: 'Alice'alone. - Always validate the output JSON in a separate parser to confirm round-trip safety.
Try it nowβ
The full js-object-to-json runs in your browser at https://ztools.zaions.com/js-object-to-json β no signup, no upload, no data leaves your device.
Last updated: 2026-05-05 Β· Author: Ahsan Mahmood Β· Edit this page on GitHub