Contact

Tools  /  JSON  /  JSON ↔ JS Object Literal

JSON ↔ JS Object Literal

Convert between strict JSON and a JS object literal both ways -- quote/unquote keys, single or double string quotes, trailing commas, sort keys, wrap as const / module.exports / export default / TypeScript "as const", best-effort repair of broken input, live conversion with auto-detected direction, syntax highlighting and a line/column error location. Nothing is uploaded.

What this tool does

Paste JSON or a JS object literal and it converts to the other one, in either direction — the conversion runs live as you type, and the Swap button sends the output back through as new input to flip direction instantly. Most converters online only go one way (JS object → JSON); this one is built to go both.

Why a JS object literal isn't JSON

They look almost identical, which is exactly what causes the errors. JSON requires double-quoted keys and strings, allows no trailing comma after the last item, and has no concept of a comment. A JS object literal allows all of that — unquoted keys when they're valid identifiers, single or double quotes, trailing commas, comments — which is exactly why JSON.parse rejects perfectly normal-looking JavaScript, and why copying an API response straight into a .js file doesn't just work.

Converting JSON into code you can paste straight in

Going from JSON to JS, the output isn't just re-quoted — it can be wrapped as ready-to-use code:

  • const x = {...} — a plain local variable, with the name you choose.
  • module.exports = {...} — for a CommonJS file.
  • export default {...} — for an ES module.
  • const x = {...} as const — TypeScript's narrowest possible type for the literal, instead of the widened type you'd get from a plain assignment.

Quote style (single or double), indentation (2 spaces, 4 spaces, or a tab), trailing commas, whether keys always get quoted, and whether keys get sorted are all controlled separately, and every preference is remembered for next time.

Fixing input that's almost right

Pasting real-world JavaScript often means hitting all of JS's extra allowances at once. Try to repair strips comments, converts single-quoted strings to double, quotes bare object keys, and removes trailing commas — the same fixes you'd otherwise make by hand before either direction can parse it. When conversion still fails, the error is mapped to a line and column instead of a raw character position.

Privacy

Both the conversion and the repair step happen entirely in your browser. Nothing you paste, drop, or load from a file is ever sent to a server.

Frequently asked questions

Is my data uploaded anywhere?
No. Both directions of conversion, the repair step, and file loading all run locally in your browser — nothing is sent to a server.
What is actually different between a JS object literal and JSON?
JSON requires double-quoted keys and strings, allows no trailing comma after the last item, and has no comments. A JS object literal allows unquoted keys (when they’re valid identifiers), single or double quotes, trailing commas, and comments — which is why pasting one into a strict JSON parser fails even though it looks almost the same.
What does wrapping as "as const" do?
It’s TypeScript syntax that gives the literal its narrowest possible type — a specific string like "active" instead of the general string type it would widen to from a plain assignment. It’s the standard way to turn JSON sample data into a strictly-typed constant.
Can I convert JSON to a JS object, not just the other way around?
Yes — this tool goes both directions. Use the JSON → JS object / JS object → JSON buttons to pick a direction directly, or convert once and hit Swap to send the result back through as new input.
Does it handle single quotes, unquoted keys and trailing commas automatically?
Converting JS object literal → JSON, yes — those are exactly the differences it resolves. If something still fails to parse (a comment, a stray semicolon), Try to repair strips the common cases before conversion runs again.
Can I load a file instead of pasting?
Yes — drag and drop a .json, .js or .txt file onto the input panel, or use the load button. The direction is set automatically from the file extension.
Theme Accent