JavaScript Object to JSON Converter
Paste a JS object literal — unquoted keys, single quotes, trailing commas, comments — get valid JSON
Options
Indent
Paste a JavaScript object literal above to convert it to JSON.
Supports unquoted keys, single quotes, trailing commas, comments, and hex/octal/binary numbers.
You copied a JavaScript object from a codebase — unquoted keys, single quotes around strings, trailing commas after the last property, maybe even a comment. You paste it into Postman as a request body and get “Invalid JSON.” The fix is mechanical: double-quote every key, replace single quotes with double quotes, strip trailing commas, remove comments. For a 50-field object, that’s 15 minutes of find-and-replace that a converter does instantly.
Why This Converter (Not the JSON Formatter)
The JSON Formatter expects already-valid JSON as input. This converter accepts JavaScript object literals — the looser syntax with unquoted keys, single quotes, trailing commas, and comments — and produces valid JSON. Use this converter to fix the syntax; use the formatter when you already have valid JSON that just needs indentation.
What Is a JavaScript Object Literal?
A JavaScript object literal is a convenient way to define an object directly in code using key-value pairs enclosed in curly braces. Unlike JSON (JavaScript Object Notation), which is a strict data-interchange format, JS object literals allow several syntactic shortcuts that make them easier to write by hand.
// JavaScript object literal — valid JS, NOT valid JSON
const config = {
host: 'localhost', // unquoted key, single-quoted value
port: 3000, // trailing comma
debug: true, /* block comment */
tags: ['web', 'api'],
}
The equivalent valid JSON would be:
{
"host": "localhost",
"port": 3000,
"debug": true,
"tags": ["web", "api"]
}
How This Converter Works
This tool parses your JavaScript object literal entirely in the browser and converts it to strict, valid JSON. Paste your JS object literal, configure options, and get the JSON output instantly — nothing is sent to any server.
What It Handles
Unquoted Keys
JSON requires all object keys to be double-quoted strings. In JS object literals, keys can be bare identifiers:
// JS
{ name: "Alice", maxRetries: 3 }
// JSON output
{ "name": "Alice", "maxRetries": 3 }
Single-Quoted Strings
JSON only accepts double quotes. Single quotes are common in JavaScript:
// JS
{ greeting: 'Hello, world!', file: 'config.json' }
// JSON output
{ "greeting": "Hello, world!", "file": "config.json" }
Template Literals (without interpolation)
Simple backtick strings (without ${...} expressions) are also supported:
// JS
{ message: `Ready`, path: `/api/v1` }
// JSON output
{ "message": "Ready", "path": "/api/v1" }
Template literals with interpolation (${variable}) are not supported because their values cannot be resolved at parse time.
Trailing Commas
JSON does not allow a comma after the last element in an object or array. JS object literals often include them for cleaner diffs:
// JS
{
alpha: 1,
beta: 2, // trailing comma — valid JS, invalid JSON
}
// JSON output
{
"alpha": 1,
"beta": 2
}
Comments
Both // line comments and /* */ block comments are stripped before conversion:
// JS
{
// Server settings
host: 'localhost',
port: 8080, /* default port */
}
// JSON output
{
"host": "localhost",
"port": 8080
}
Alternative Number Formats
JavaScript supports number literals that JSON does not. This tool converts them to standard decimal:
| JS Literal | Description | JSON Output |
|---|---|---|
0xFF | Hexadecimal | 255 |
0o17 | Octal | 15 |
0b1010 | Binary | 10 |
1_000_000 | Numeric separator | 1000000 |
.5 | Leading-dot float | 0.5 |
5. | Trailing-dot float | 5 |
+42 | Unary plus | 42 |
Numeric Object Keys
JavaScript allows numeric keys in object literals — they are always stored as strings. The converter quotes them automatically:
// JS
{ 0: 'zero', 1: 'one', 200: 'ok' }
// JSON output
{ "0": "zero", "1": "one", "200": "ok" }
What Gets Flagged as Errors
Not all JS values can be represented in JSON. The converter shows a clear error for:
undefined— not a JSON value (can optionally be converted tonullusing the undefined → null option)NaN— not a JSON valueInfinityand-Infinity— not JSON values- Template literal interpolation (
${expr}) — cannot be evaluated statically - Unexpected identifiers — function names, class references, or other runtime expressions
Options
Indent Size
Choose 2 spaces (default) or 4 spaces for the JSON output indentation. Both are valid JSON and widely used in configuration files.
undefined → null
When enabled (default), JavaScript undefined values are automatically converted to JSON null. When disabled, the converter reports an error instead — useful if you want to confirm that your data contains no undefined values before serializing.
Common Use Cases
Copying from browser DevTools: When you inspect an object in the browser console, you often get JS-style output with unquoted keys. Paste it here to get clean JSON.
Migrating config files: JavaScript config files (e.g., webpack.config.js, .eslintrc.js) use object literal syntax. This converter helps you extract the data portion as JSON.
API payload construction: When building HTTP request bodies in JavaScript, you might write the payload as a JS object literal first. Convert it to JSON for use in documentation or test tools like Postman.
Data transformation: Convert hardcoded JavaScript data arrays and objects to JSON files for use in other languages and environments.
Sanitizing copy-pasted data: Data copied from JavaScript source code or console.log() output often contains JS-specific syntax that breaks standard JSON parsers.
JSON Specification Differences
| Feature | JS Object Literal | JSON |
|---|---|---|
| Unquoted keys | ✅ | ❌ |
| Single-quoted strings | ✅ | ❌ |
| Template literals | ✅ | ❌ |
| Trailing commas | ✅ | ❌ |
Comments (//, /* */) | ✅ | ❌ |
undefined as value | ✅ | ❌ |
NaN | ✅ | ❌ |
Infinity | ✅ | ❌ |
| Hex/octal/binary numbers | ✅ | ❌ |
Numeric separators (1_000) | ✅ | ❌ |
| All keys must be strings | ✅ | ✅ |
Frequently Asked Questions
Why does my JS object fail to parse?
The most common causes are: template literal interpolation (${variable}), Infinity or NaN values, references to variables or function calls, or syntax errors in the original JS. The error message will point to the specific issue.
Can I paste output from console.log()?
Often yes — console.log of plain objects and arrays typically produces output very close to JS literal syntax. However, if the object contains non-serializable values (functions, Symbols, circular references), those entries will be omitted in the log but may appear as unexpected tokens here.
Does this support JSON5 or HJSON? This tool covers the most common JavaScript object literal patterns. It is not a full JSON5 or HJSON parser but handles all features found in typical hand-written JS config objects and API payloads.
What happens to functions or class instances in the object?
Function values, class constructors, and Symbol keys cannot be represented in JSON. If present, the converter will throw an error at that token. Remove or replace these values with serializable equivalents.
Is my data private? Yes. All parsing and conversion happens entirely in your browser. No data is transmitted to any server, stored, or logged.
Can I convert a JSON array instead of an object?
Yes. The converter accepts any valid JS literal as the root value — an object {}, an array [], a string, number, boolean, or null.