HomeFormattersCodeJavaScript Object to JSON Formatter

JavaScript Object to JSON Formatter

Code

Convert a JavaScript object literal to valid JSON instantly. Handles unquoted keys, single quotes, trailing commas, and comments. In-browser — nothing uploaded.

Reviewed by the thecalcu.com team · Last updated July 26, 2026

What is a JS→JSON?

The JavaScript Object to JSON Formatter converts a JavaScript object literal, with unquoted keys, single-quoted strings, trailing commas, and comments, into valid, formatted JSON. It bridges the gap between JavaScript developer shorthand and the strict JSON format required by APIs, configuration files, and storage systems.

JavaScript object literal syntax is more permissive than JSON: keys don't need to be quoted, strings can use single quotes, trailing commas are allowed, and comments are supported. Developers often write configuration, test fixtures, or mock data using this relaxed JavaScript syntax, then need to convert it to JSON for deployment or API usage.

The formatter applies a series of transforms in sequence:

  1. Strip comments, removes // line comments and /* */ block comments
  2. Convert single to double quotes, replaces single-quoted strings with double-quoted JSON strings
  3. Quote bare keys, adds double-quotes around unquoted object keys
  4. Remove trailing commas, removes commas before } or ]
  5. Parse and reformat, validates the result with JSON.parse and outputs with JSON.stringify

What it cannot convert: function values, undefined, Symbol, regular expressions, new Date(), Infinity, NaN, or circular references, these have no JSON equivalent. The formatter returns a clear error message for these cases.

Related tools: JSON Formatter for formatting already-valid JSON, JSON to TypeScript Interface for generating TypeScript interfaces from JSON.

All processing is client-side. No data is transmitted.

Why Use a JavaScript Object to JSON Formatter?

The need to convert JS objects to JSON comes up constantly in web development: copying a configuration object from a JavaScript file and needing it in package.json format, pasting a mock data fixture into an API testing tool, converting a Prettier config written as JS syntax into a .prettierrc.json file.

Developer workflow use case: An Indian full-stack developer is writing end-to-end tests using Playwright and has defined test data as JavaScript objects with single-quoted strings and unquoted keys. The test runner needs the data in a JSON file for parameterised runs. Pasting the JS object into the formatter converts it to valid JSON in one step.

Configuration use case: A Next.js developer has a next.config.js file with the configuration as a JS object. To document the configuration in a README or convert it to a next.config.json format, the formatter handles the syntax differences automatically.

Who Should Use This Formatter?

Frontend and full-stack developers who regularly work with JavaScript/TypeScript configuration objects that need to be converted to JSON for deployment, documentation, or API use.

Indian development teams building Node.js, React, or Next.js applications where configuration is written in JavaScript syntax but needs to be in JSON format for certain tooling or APIs.

QA engineers maintaining test fixtures written as JavaScript objects that need to be converted to JSON for parameterised test runs or API testing tools like Postman.

DevOps engineers converting JavaScript-format configuration objects (from webpack configs, Babel configs, ESLint configs) into JSON format for CI/CD pipelines or tooling that requires strict JSON.

What Insights Does the JS Object to JSON Formatter Give You?

The output is valid, formatted JSON with the selected indent style. If conversion succeeds, the JSON is guaranteed to parse without errors in any compliant JSON parser. If conversion fails (due to functions, undefined, or other non-serialisable values), the error message specifies the problem type with a hint about what to fix.

How to use this JS→JSON calculator

  1. Paste the JavaScript object into the input textarea, any valid JS object literal syntax.
  2. Select the indent style, 2 spaces, 4 spaces, tab, or minified.
  3. Check the output panel, valid JSON appears instantly; errors show in the output with explanation.
  4. Copy the JSON output using the copy button.
  5. If conversion fails: check for functions, undefined, template literals, or new Date(), remove or replace these before converting.
  6. Related tools: use the JSON Formatter if the input is already valid JSON.
Show formula & methodology ↓Show less ↑

Formula & Methodology

Conversion steps applied in order:

1. Strip // line comments 2. Strip /* block comments */ 3. Convert 'single-quoted strings' → "double-quoted strings"    (handles escaped single quotes inside: \' → ') 4. Quote bare keys: { key: → { "key":    (regex: /([{,]\s*)([a-zA-Z_$][a-zA-Z0-9_$]*)(\s*:)/g) 5. Remove trailing commas: , } → } and , ] → ] 6. JSON.parse(result) → validates; catches any remaining syntax errors 7. JSON.stringify(parsed, null, indent) → canonical formatted output

Before and after example:

javascript // JavaScript object (input) {   name: 'Alice',  // user name   age: 30,   tags: ['admin', 'user'], } 

json {   "name": "Alice",   "age": 30,   "tags": ["admin", "user"] } 

Frequently Asked Questions

What is the difference between a JavaScript object and JSON?
JavaScript object literals and JSON look similar but have key differences. JSON keys must be double-quoted strings; JavaScript object literals allow unquoted bare keys. JSON strings must use double quotes; JavaScript allows single quotes. JSON does not allow trailing commas; JavaScript objects do. JSON does not support comments; JavaScript supports // and /* */. JSON does not support undefined, functions, Dates, or symbols, these cannot be serialised to JSON.
When do you need to convert a JavaScript object to JSON?
You need to convert a JS object to JSON when sending data in an HTTP request body (APIs accept JSON, not raw JS syntax), storing configuration in a JSON file, persisting data to localStorage (which stores strings, not objects), using JSON.stringify in code, or sharing data with systems that accept only standard JSON. Developers often write configuration or test data as JS objects (with unquoted keys and single quotes) but need to convert them to valid JSON before use.
Why does the formatter fail on some JavaScript objects?
The formatter cannot convert JavaScript features that have no JSON equivalent: function values (functions cannot be serialised), undefined values (undefined is not a valid JSON value, JSON has only null), Symbol values, regular expressions (RegExp objects), Dates (a Date object is serialised as an ISO string, but a `new Date()` expression cannot be evaluated), and circular references. If the object contains any of these, the formatter shows an error message.
Does the formatter support single-quoted strings?
The formatter converts single-quoted string values to double-quoted JSON strings. For example, `name: 'Alice'` becomes `"name": "Alice"`. It also handles escaped single quotes inside single-quoted strings: `'it\'s'` becomes `"it's"`. However, template literals (backtick strings) are not supported.
Does the formatter remove trailing commas?
Trailing commas after the last property in an object or the last item in an array are removed. Trailing commas are valid JavaScript but invalid JSON. For example, `{ name: 'Alice', }` becomes `{"name": "Alice"}` in the JSON output.
Does the formatter strip JavaScript comments?
Both single-line comments (`// comment`) and block comments (`/* comment */`) are removed before conversion. This is useful when you have annotated JavaScript configuration objects that you need to convert to JSON for deployment.
Is my JavaScript code stored when I use this formatter?
No, all processing runs entirely in your browser. The JavaScript object is never sent to any server, stored, or logged. The tool works offline once loaded.
How do I use the JS Object to JSON Formatter?
Paste your JavaScript object literal into the input textarea. The formatter strips comments, converts single quotes to double quotes, quotes bare keys, removes trailing commas, and then uses JSON.parse / JSON.stringify to produce valid, formatted JSON. Select the indent level (2 spaces, 4 spaces, tab, or minified) and copy the output.
What indent options are available?
Four indent options are available: 2 spaces (standard for most JavaScript/TypeScript projects), 4 spaces (common in Python and some enterprise codebases), tab (used by some style guides and accessibility preferences), and minified (no whitespace, compact single-line JSON for transmission or storage where size matters).
How is this different from the JSON Formatter?
The [JSON Formatter](/json-formatter/) accepts valid JSON as input and reformats it (pretty-print, minify). It will reject JavaScript object syntax with unquoted keys or single quotes. The JS Object to JSON Formatter accepts JavaScript object literal syntax and converts it to valid JSON first, then formats it. Use this tool when the input is JavaScript code; use the JSON Formatter when the input is already valid JSON.
Can this formatter handle very deeply nested objects?
Yes, the formatter handles deeply nested objects and arrays by recursively processing the input through JSON.parse and JSON.stringify. The depth limit is determined by the JavaScript engine's stack depth, which is more than sufficient for any practical configuration or data object. Very large objects (hundreds of kilobytes of text) may take a moment to process but will not crash the browser.
Also known as
JS object to JSONJavaScript to JSON converterconvert object to JSONparse JS objectstringify JavaScript