The rule: every key is a double-quoted string
The JSON grammar in RFC 8259 is short. An object is {, zero or more members separated by commas, then }. A member is a string, a colon, and a value. A string always starts and ends with ". So a key cannot be a bare word, a number or a single-quoted string:
| Key written as | JavaScript literal | JSON5 | JSON |
|---|---|---|---|
{"name": 1} | valid | valid | valid |
{name: 1} | valid | valid | invalid |
{'name': 1} | valid | valid | invalid |
{$id: 1}, {_x: 1} | valid | valid | invalid |
{42: "a"} | valid (key becomes "42") | invalid (JSON5 keys are identifiers or strings) | invalid |
{first-name: 1} | invalid (the minus sign is an operator) | invalid | invalid |
{[key]: 1} (computed) | valid | invalid | invalid |
The fix is always the same: wrap the key in double quotes. {"first-name": 1} and {"42": "a"} are valid JSON.
JavaScript object literal vs JSON
The two look alike because JSON was derived from JavaScript, but they are different things. An object literal is code: it is evaluated, so it can contain variables, function calls, comments, methods and computed keys. JSON is data: a fixed text format that Python, Go, Java, databases and command-line tools all read. Accepting one key form keeps every parser simple and consistent.
// JavaScript source: fine
const user = { id: 42, name: "Ada", tags: ["admin"], };
// JSON text: what an API or a .json file must contain
{"id": 42, "name": "Ada", "tags": ["admin"]}
In running code you never quote keys yourself. JSON.stringify(user) writes them correctly. Unquoted keys only become a problem when someone copies a literal out of source code, a browser console, a config file or an LLM answer and treats it as JSON.
Error messages for unquoted keys
For input such as {name: "Ada"}:
| Where | Error message |
|---|---|
| Chrome, Edge, Node.js (current V8) | SyntaxError: Expected property name or '}' in JSON at position 1, and newer versions add (line 1 column 2) |
| Older V8 (Node.js 18 and earlier) | SyntaxError: Unexpected token n in JSON at position 1 |
| Firefox | SyntaxError: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data |
Python json.loads | json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1) |
Go encoding/json | invalid character 'n' looking for beginning of object key string |
| Java (Jackson) | Unexpected character ('n' (code 110)): was expecting double-quote to start field name |
The position always points at the first character of the first bad key. In a long document that is the place to look, and Find the error above shows the surrounding lines with a caret under it. Python's message also appears for a trailing comma before } in older Python versions, because after a comma the parser expects another key. Recent versions say Illegal trailing comma before end of object instead.
Converting: worked example
Before (a typical copy from source code):
const config = {
port: 8080,
host: 'localhost',
// retry settings
retry: { attempts: 3, backoffMs: 250, },
};
After Convert / Repair:
{
"port": 8080,
"host": "localhost",
"retry": {
"attempts": 3,
"backoffMs": 250
}
}
The converter dropped the const config = assignment and the semicolon, quoted the keys, changed the single quotes, removed the comment and both trailing commas. Every change is listed so you can check it.
Doing it in code
You have the object at runtime: just serialise it.
const json = JSON.stringify(obj, null, 2);
You have JSON5 or object-literal text: parse it with a tolerant parser, then stringify.
// npm install json5
import JSON5 from "json5";
const json = JSON.stringify(JSON5.parse(text), null, 2);
# Python: pip install json5
import json, json5
fixed = json.dumps(json5.loads(text), indent=2)
Java: Jackson can read them with JsonReadFeature.ALLOW_UNQUOTED_FIELD_NAMES enabled on the mapper. It still writes standard, quoted JSON.
Avoid eval("(" + text + ")") or new Function. They do turn a literal into an object, but they also run any code hidden in the text. A regex that adds quotes is fragile for the reason given in the FAQ below.
Keys that need care
- Duplicate keys are allowed by the grammar, but most parsers keep only the last value. The converter warns when it sees one.
- Number-like keys become strings:
{1: "a"}turns into{"1": "a"}, which is also whatJSON.stringifyproduces from that literal. - Keys with dots or dashes such as
a.borx-idare kept as one key. JSON has no nested-path syntax.
FAQ
Do JSON keys have to be in quotes?
Yes. RFC 8259 defines an object member as a string, a colon and a value, and a JSON string is always written in double quotes. {name: 1} is a valid JavaScript object literal but not valid JSON, and every strict parser rejects it.
Why do JavaScript objects allow unquoted keys but JSON does not?
JavaScript object literals accept any identifier, number or string as a key. JSON was designed as a minimal data format that every language could parse the same way, so it allows exactly one key form. Quoting every key also avoids reserved-word problems, such as an unquoted key named class or default in old JavaScript engines.
Can numbers be JSON keys?
Only as strings. {"1": "a"} is valid, {1: "a"} is not. JSON.parse returns the key as the string "1", and Python's json.dumps converts integer dict keys to strings for the same reason.
How do I convert a JavaScript object with unquoted keys to JSON?
If the object exists in running code, call JSON.stringify(obj). If you only have the source text, paste it into the converter above, or parse it with a JSON5 parser and stringify the result. Avoid eval() on text you did not write.
Does JSON5 allow unquoted keys?
Yes. JSON5 allows keys that are valid ECMAScript identifiers, such as name, $id or _x, without quotes. It also allows single quotes, trailing commas and comments. It is not accepted by JSON.parse or by most APIs.
Can a regex add the missing quotes?
For simple, flat input a regex like /([{,]\s*)([A-Za-z_$][\w$]*)\s*:/g gets close, but it also changes text inside string values such as "time {hh: mm}" and URLs like "http://x". A parser-based converter only touches real keys.