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?
What is actually different between a JS object literal and JSON?
What does wrapping as "as const" do?
"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.