Xerobit

Convert a JavaScript Object to JSON

Paste a JavaScript object literal into the input box to convert it to valid JSON. The converter handles single-quoted strings, unquoted property names, trailing commas, and comments so you get standards-compliant output. Everything runs locally in your browser and nothing is uploaded to any server.

Paste a JS object literal here.

JSON Output

{
  "name": "Alice",
  "age": 30
}

By · Published · How we build and test tools

How to use the javascript object to json converter

  1. Paste your JavaScript object literal into the JavaScript object text area.
  2. Choose the number of indent spaces (use 0 for compact, minified JSON).
  3. The valid JSON output appears automatically as you type or paste.
  4. Copy the result using the copy button provided below the output.

How the conversion works

A JavaScript object literal is not the same as JSON. JSON requires double-quoted keys and string values, forbids trailing commas, and does not support comments or undefined. This javascript object to json converter tokenizes the input character by character to handle these differences safely.

The parser reads the text itself and never runs it as code. It understands strings (single, double or backtick quoted without ${}), numbers (including hex, octal, binary and digit separators), unquoted keys, the literals true, false and null, and an optional const x =, module.exports = or export default prefix. Variables, functions, spread syntax and computed keys cannot be turned into JSON and produce an error. It strips single-line (//) and multi-line (/* */) comments during tokenization. Trailing commas before closing braces or brackets are detected and removed from the token stream before the final JSON string is assembled.

Differences between JS objects and JSON

Understanding what changes during conversion helps avoid surprises. Here is a reference table of common differences:

FeatureJavaScript ObjectJSON
KeysUnquoted, single, or double quotedDouble quoted only
StringsSingle or double quotesDouble quotes only
Trailing commasAllowedForbidden
Comments// and /* */Not allowed
UndefinedValid valueNot supported
NaN / InfinityValid numbersNot supported

When you convert a js object to json, all single quotes wrapping strings or keys become double quotes, and any trailing commas are silently dropped so the output passes strict validation.

Handling nested structures

Nested objects and arrays are parsed recursively. When the tokenizer encounters an opening brace or bracket, it tracks the nesting depth. A comma is only considered trailing if the very next non-whitespace, non-comment token is a closing brace or bracket at the same depth.

For example, given { a: [1, 2,], b: { c: 3, } }, the converter removes the comma after 2 and the comma after 3, producing valid JSON while preserving the structure exactly. The indentation level increases by the configured number of spaces for each nested layer.

Common errors and edge cases

The most frequent error when converting a javascript object to json is including undefined or functions. JSON has no representation for either, so the converter returns an error rather than silently dropping data or producing invalid output.

Another edge case involves escaped quotes inside strings. A single-quoted string like 'it\'s' is converted to "it's" because JSON does not require escaping single quotes. Conversely, double-quoted strings retain their internal escaped double quotes. Unmatched quotes or unclosed brackets will also trigger a parse error indicating the position where parsing failed.

Worked examples

ExampleInputResult
Simple objectJavaScript object: { name: 'Alice', age: 30 }; Indent spaces: 2{ / "name": "Alice", / "age": 30 / }
Nested with trailing commaJavaScript object: { / user: { / id: 1, / active: true, / }, / roles: ['admin', 'editor'], / }; Indent spaces: 2{ / "user": { / "id": 1, / "active": true / }, / "roles": [ / "admin", / "editor" / ] / }
Minified outputJavaScript object: { x: 10, y: 20 }; Indent spaces: 0{"x":10,"y":20}

FAQ

Why is my JavaScript object not valid JSON?

JSON requires double quotes around all keys and string values, and it does not allow trailing commas, comments, or the value undefined. The converter fixes quotes and commas automatically but will return an error for unsupported values.

Does this tool handle single-quoted strings?

Yes. Single-quoted strings are parsed correctly and converted to double-quoted strings in the JSON output. Escaped single quotes inside them are unescaped since JSON does not require escaping single quotes.

Can I convert an array instead of an object?

Yes. You can paste a JavaScript array literal such as [1, 'two', { three: 3 }] and it will be converted to a valid JSON array.

What happens to trailing commas?

Trailing commas before a closing brace or bracket are removed during tokenization. This ensures the resulting JSON is strictly valid according to the specification.

Is my data sent to a server?

No. The entire javascript object to json conversion runs locally in your browser using JavaScript. Nothing is uploaded or transmitted over the network.

How do I get minified JSON output?

Set the indent spaces field to 0. The converter will output the JSON on a single line with no extra whitespace between tokens.