Here is the snippet that starts most of these sessions:
{name: "Maya", age: 31, city: "Austin"}
Paste that into a browser console and it works. It is a fine JavaScript object literal. Hand the same text to JSON.parse and you get an error, because JSON is a smaller language than JavaScript. In JSON, a key is not a bare identifier. A key is a string, written with double quotes, followed by a colon. The parser reads n, expects a quote, finds none, and stops. Chrome in recent versions says something like "Expected double-quoted property name in JSON at position 1". Python says "Expecting property name enclosed in double quotes". Both messages point at the same missing quote.
That distinction is worth sitting with, because it explains where unquoted keys come from. Someone copies an object from a console, a config file, or a code sample. Someone else writes JSON by hand and treats it like JavaScript because the two look alike. A language model outputs an object literal when you asked for JSON. In every case the writer was thinking in JavaScript and the reader is strict JSON. The fix is conversion, not persuasion. You change the text, or you change the tool that produces it.
How to fix JSON unquoted keys by hand
For a short snippet, quote every key. The example above becomes {"name": "Maya", "age": 31, "city": "Austin"}. That is the whole fix, and for a config with a dozen keys it is faster than any tooling debate. Work from the outside in: quote the keys first, then check the values. Unquoted keys often travel with friends, single quotes around strings, trailing commas, and comments, and fixing only the keys leaves you with a second error five seconds later.
Watch the keys that look simple but are not. A key with a space, a hyphen, or a digit at the front, such as first-name or 2fa-enabled, must be quoted even in JavaScript. A key that is a reserved word is fine quoted in JSON and usually fine unquoted in modern JavaScript, but quoted is still the safe form. If you are converting by hand, quote everything and stop thinking about which keys could skip quotes. Consistency beats cleverness in a data format.
The regex fix, and where it breaks
For a longer file, a regex that quotes bare identifiers is the standard shortcut. In JavaScript it looks like replacing /([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:/g with "$1":, applied only outside strings. That last clause is the part people skip, and skipping it is how a regex fix creates a new bug. A URL value such as "site": "https://example.com" contains a colon after a word, and a naive pattern can quote inside a string value. A string value that itself contains a word followed by a colon, like a time stamp written as prose, is another trap.
My rule is simple. Use the regex only on text you can eyeball afterward, and run the result through a real parser immediately. If the file is longer than a screen or two, skip the regex entirely and use a tolerant parser instead. JSON5 accepts unquoted keys, single quotes, trailing commas, and comments, and its parser can hand you a real object. Python developers have a parallel option: ast.literal_eval reads a Python-style literal safely, without running code the way eval would. Either route gives you an object you can then serialize with a strict serializer, which is the point. The tolerant parser is a bridge, not a destination. The output of the bridge should be strict JSON.
The fix that stops it coming back
Every unquoted-key bug I have seen twice had the same root cause: someone was building JSON by hand, with string concatenation or a template. The durable fix is to stop. Build the object in your language, then serialize it. In JavaScript that means JSON.stringify. In Python it means json.dumps. In Go it means encoding/json. A serializer quotes every key, escapes every string, and never forgets a comma, because quoting keys is not a decision it leaves to you.
That rule also settles the "should I keep it as JavaScript?" question. If the consumer is your own code and nothing else reads the file, a JavaScript module or a JSON5 config is a legitimate choice, and plenty of tools use JSONC for exactly that reason. But the moment the file crosses a boundary, an API, a queue, another team's service, a file a user downloads, it should be strict JSON. Convert at the boundary, with a serializer, and keep the relaxed format on your side of the line. A worked example makes the workflow concrete. You receive {id: 482, status: "active", plan: "pro"} from a log. You parse it with a JSON5 parser, you get an object with three keys, you stringify it, and you send {"id": 482, "status": "active", "plan": "pro"} onward. Three steps, no regex, no guessing about which keys were quoted already.
One last check before you ship a repaired file. Parse it twice: once with the strict parser your consumer uses, and once after a round trip through stringify and parse again. If both pass and the key count matches the original, you are done. If the count differs, a key was lost in conversion, usually a duplicate key or a key the tolerant parser normalized, and that is worth finding before your consumer does.
Frequently asked questions
Why does JSON require quoted keys when JavaScript does not?
Because JSON is a separate, smaller format with its own grammar. In that grammar a member name is always a string. JavaScript object literals are more permissive, so text that is valid JavaScript is often invalid JSON.
What error does an unquoted key produce?
Chrome and Node report an expected double-quoted property name error near the first unquoted key. Python reports that it expected a property name enclosed in double quotes. Both point at the key, not at the value after it.
Can a regex safely quote all unquoted keys?
Only on short text you verify afterward. A naive pattern can match colons inside string values, such as URLs. For longer files use a tolerant parser like JSON5 and then serialize with a strict serializer.
Is JSON5 a replacement for JSON?
No. JSON5 is a useful bridge for reading relaxed input. Data that leaves your system should still be strict JSON, produced by a serializer, so every consumer can read it.
What if only some keys are unquoted?
Quote the rest and parse again. Mixed files are common when a snippet is edited by hand. A strict parse after the fix is the only check that matters.
Get one practical guide a week: subscribe to the newsletter for new guides in this series.
Related reading: How to Fix the JSON Single Quotes Error · The Trailing Comma That Breaks Your JSON · JSON with Comments (JSONC): What It Is and How to Handle It