ENRU

Unquoted key in JSON: “Expected property name”

Updated

A key without quotes is just a rumour. In JSON every object key is a string, and a string is always in double quotes: {"name": 1}, never {name: 1}, even when the key is a plain word or a number.

The error and the fix

This is valid JavaScript and invalid JSON. It usually arrives from code, a console or a config written by someone who thinks in JavaScript:

{
  id: 42,
  status: "ready",
  2: "second shift"
}

Every key gets double quotes, the number key included. Values stay as they were:

{
  "id": 42,
  "status": "ready",
  "2": "second shift"
}

If the values use single quotes too, the text is a JavaScript or Python literal through and through; fix the single quotes as well, or better, regenerate the text with a serializer.

Why JSON keys must be quoted

RFC 8259, section 4: “A name is a string”, and the grammar spells a member as member = string name-separator value. There is no rule for an identifier, so name without quotes is not a key, not a value, nothing a JSON parser can place.

JavaScript object literals allow bare identifiers and numbers as keys, but JSON was cut from JavaScript deliberately small: no identifier rules, no reserved words, no question whether class or 2 or café needs quotes. With quotes, the answer is always yes. That is also why a JSON key can be any text at all: spaces, dots, emoji.

Unquoted key error messages by parser

An unquoted first key, {name: 1}, and an unquoted key after a comma, {"a": 1, b: 2}. Several parsers word the two cases differently, and none of them uses the word “unquoted”.

ParserFirst key: {name: 1}After a comma: {"a": 1, b: 2}
Chrome, Edge, Node.js (V8)Expected property name or '}' in JSON at position 1 (line 1 column 2)Expected double-quoted property name in JSON at position 9 (line 1 column 10)
Node.js 18, older Chrome (V8 10.2)Unexpected token n in JSON at position 1Unexpected token b in JSON at position 9
FirefoxJSON.parse: expected property name or '}' at line 1 column 2 of the JSON dataJSON.parse: expected double-quoted property name at line 1 column 10 of the JSON data
SafariJSON Parse error: Expected '}'JSON Parse error: Property name must be a string literal
PythonExpecting property name enclosed in double quotes: line 1 column 2 (char 1)Expecting property name enclosed in double quotes: line 1 column 10 (char 9)
Go (encoding/json)invalid character 'n' looking for beginning of object key stringinvalid character 'b' looking for beginning of object key string
Java (Jackson 2)Unexpected character ('n' (code 110)): was expecting double-quote to start field nameUnexpected character ('b' (code 98)): was expecting double-quote to start field name
C# (System.Text.Json)'n' is an invalid start of a property name. Expected a '"'.'b' is an invalid start of a property name. Expected a '"'.
C# (Newtonsoft.Json)No error: unquoted keys are acceptedNo error: unquoted keys are accepted
PHP (json_decode)Syntax error, json_last_error() 4Syntax error, json_last_error() 4
Rust (serde_json)key must be a string at line 1 column 2key must be a string at line 1 column 10
jqjq: parse error: Invalid numeric literal at line 1, column 6jq: parse error: Invalid numeric literal at line 1, column 11

Safari’s Expected '}' for the first key is misleading: nothing is wrong with the braces. jq’s Invalid numeric literal means it tried to read the bare word as a number. Checked by running: Node.js 18, 24 and 25.1, Safari 27, Python 3.9 and 3.14, Go 1.25–1.27, Jackson 2.22 (Jackson 3 says “property name”), System.Text.Json and Newtonsoft.Json 13.0.4, PHP 8.3 and 8.5, serde_json 1.0, jq 1.7 and 1.8; Firefox from its source code and Mozilla’s reference.

Where unquoted keys come from

  • console.log in Node.js. It prints { id: 42, items: [ 'tea' ] }: an inspection format for people, with bare keys and single quotes.
  • Object literals copied from code: test fixtures, Storybook stories, a snippet from documentation.
  • Database shells and other tools that print documents in their own JavaScript-like notation.
  • Hand-written configs by people used to JSON5 or YAML, where bare keys are fine.

How to get quoted keys in code

A serializer quotes every key, including numbers: map and dictionary keys come out as strings. That is also the catch on the way back: after a round trip through JSON, the key 1 is the string "1".

JavaScript

const order = { id: 42, items: ["tea"] };
console.log(order);                 // { id: 42, items: [ 'tea' ] } is inspection output
console.log(JSON.stringify(order)); // {"id":42,"items":["tea"]}

Python

import json

text = json.dumps({1: "one", 2: "two"})
print(text)              # {"1": "one", "2": "two"}
print(json.loads(text))  # keys come back as strings: '1', '2'

Go

package main

import (
	"encoding/json"
	"fmt"
)

type Order struct {
	ID    int      `json:"id"`
	Items []string `json:"items"`
}

func main() {
	b, _ := json.Marshal(Order{ID: 42, Items: []string{"tea"}})
	fmt.Println(string(b)) // {"id":42,"items":["tea"]}
	b, _ = json.Marshal(map[int]string{1: "one"})
	fmt.Println(string(b)) // {"1":"one"}
}

Java (Jackson)

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.TreeMap;

Map<Integer, String> m = new TreeMap<>(Map.of(1, "one", 2, "two"));
System.out.println(new ObjectMapper().writeValueAsString(m)); // {"1":"one","2":"two"}

C#

using System.Text.Json;

var m = new Dictionary<int, string> { [1] = "one", [2] = "two" };
Console.WriteLine(JsonSerializer.Serialize(m)); // {"1":"one","2":"two"}

PHP

echo json_encode([1 => 'one', 2 => 'two']), "\n"; // {"1":"one","2":"two"}
echo json_encode(['one', 'two']);                 // ["one","two"]: keys 0, 1 make a list

Lenient readers exist: Newtonsoft.Json and JSON5 parsers accept bare keys, and Jackson does with JsonReadFeature.ALLOW_UNQUOTED_FIELD_NAMES. Use them for files people write by hand, and never as a reason to send such text to someone else.

How to keep unquoted keys out

  • Log and copy JSON.stringify(value, null, 2), not the object itself.
  • Keep fixtures as .json files rather than object literals pasted into strings.
  • If a config is meant to be written by people, choose JSON5 or YAML openly and convert it to JSON in the build.
  • Check anything copied from a console in the JSON validator before it goes into a request.

Try it in the validator

Open a sample with an unquoted key in the JSON validator. It reports unquoted_key on the first character of the key, for Latin and non-Latin letters, digits, _ and $ alike.

Related: single quotes, missing colon, trailing comma (Chrome gives it the same message), Expecting property name enclosed in double quotes, and all JSON errors.

Unquoted keys FAQ

Do JSON keys need quotes?

Yes, always double quotes. A JSON key is a string, whatever it contains: a word, a number, a space or nothing at all ("" is a valid key).

Can a JSON key be a number?

Only as a string: {"1": "one"}. Serializers write numeric map keys that way, and parsers give them back as strings.

Why does the same text work as a JavaScript object?

Object literal syntax in JavaScript allows bare identifiers and numbers as keys. JSON looks like that syntax but is stricter: it is a data format, not code.