Developer

JS Object to JSON Converter

Convert a JavaScript object literal into valid JSON, handling unquoted keys, single quotes, trailing commas and comments.

Last reviewed by the Radiatus Cloud team

Need this built for your product?

We design, build & host secure software & APIs.

Talk to an engineer

Why a JS object is not JSON

JSON was derived from JavaScript object syntax but is far stricter. A JavaScript literal permits unquoted keys, single-quoted strings, trailing commas, comments, and values JSON has no representation for. Pasting one into a JSON parser fails immediately, which is the single most common JSON parse error in practice.

What has to change

Keys must be double-quoted. Strings must use double quotes, and any double quote inside them must be escaped. The trailing comma after the final element must go. Comments must be removed entirely, since JSON has no comment syntax at all. Each of these is mechanical, and doing them by hand on a large object is where mistakes creep in.

Values with no JSON equivalent

Some JavaScript values simply cannot be represented. undefined has no JSON form; a property holding it is dropped rather than converted. Functions and symbols likewise disappear. NaN and Infinity are valid JavaScript numbers and invalid JSON, so they must become null or a string. A Date object becomes an ISO string. Knowing which of these your object contains matters, because the conversion silently loses them rather than erroring.

Large numbers lose precision

JSON numbers are parsed as doubles, exact only to 2^53-1. A JavaScript BigInt cannot be serialised at all and throws. A 64-bit identifier held as a number silently rounds. Convert large identifiers to strings before they reach JSON, not after.

Single quotes inside strings

Converting single-quoted strings to double-quoted is not a straight character swap. A string delimited by single quotes may contain unescaped double quotes, which become syntax errors once the delimiters change. Those inner quotes need escaping as part of the conversion, and this is the step hand-editing usually misses.

JSON5 exists

If you want comments and trailing commas in a configuration file, JSON5 permits both and several tools accept it. It is not JSON, so anything expecting strict JSON will still reject it, but it is a reasonable middle ground for files humans edit.

Related tools

  • JSON Formatter — Format and beautify JSON in your browser. Pinpoints syntax errors by line and column, flags unsafe integers, and never uploads your data to a server.
  • JSON Validator — Validate JSON syntax with precise line and column errors, and check documents against a JSON Schema. Runs locally in your browser, nothing uploaded.
  • Regex Tester — Test regular expressions against sample text with live match highlighting, capture groups and flag control. Runs entirely in your browser.
  • HTML Minifier — Minify HTML by removing comments and redundant whitespace, without breaking inline elements or pre blocks. Runs entirely in your browser.

Frequently Asked Questions

Why does my JavaScript object fail to parse as JSON?

Because JSON is far stricter: it requires double-quoted keys and strings, forbids trailing commas, and has no comment syntax. All four are legal in a JavaScript literal and illegal in JSON.

What happens to undefined values?

They are dropped rather than converted, because JSON has no representation for undefined. Functions and symbols disappear the same way, silently rather than with an error.

How are NaN and Infinity handled?

They are valid JavaScript numbers and invalid JSON, so they must become null or a string. Which one is a decision the converter makes, and it changes what the consumer sees.

Do large numbers survive?

Not reliably. JSON numbers parse as doubles, exact only to 2^53-1, so 64-bit identifiers round silently. BigInt cannot be serialised at all. Convert large IDs to strings first.

Can I keep comments in a config file?

Not in JSON. JSON5 allows comments and trailing commas and is accepted by several tools, but anything expecting strict JSON will reject it.

Privacy & Security

All processing happens locally in your browser — nothing is uploaded.

Data: None
Client-side-Side
Active
v1.0

How to Use

Paste a JS object or loose JSON; valid formatted JSON is produced.

Disclaimer: This tool is provided "as is" without warranty of any kind. Results are for educational and utility purposes.