karatelabs/karate · error
Expected string key in object
Error message
Expected string key in object
What it means
Within parseObject, every key must be a double-quoted string per RFC 8259. If after skipping whitespace the next character is not `"` (or input is exhausted), the parser throws "Expected string key in object". This rejects JS-style unquoted or single-quoted keys.
Solutions
- Quote every object key with double quotes: `{"name": 1}`.
- Convert single quotes around keys to double quotes.
- Fix truncation so the object closes properly with `}` instead of ending where a key is expected.
Example fix
// before
var x = JSON.parse('{name: "a"}');
// after
var x = JSON.parse('{"name": "a"}'); Defensive patterns
Strategy: validation
Validate before calling
// detect JS-style keys before parsing
if (/'[\w-]+'\s*:|(^|\{)\s*[A-Za-z_$][\w$]*\s*:/.test(input)) {
throw new IllegalArgumentException('object keys must be double-quoted JSON strings');
} Try / catch
try {
var obj = JSON.parse(input);
} catch (e) {
if (('' + e).indexOf('Expected string key') >= 0) {
console.log('check that every object key is "quoted"');
}
throw e;
} Prevention
- Always double-quote object keys in JSON.
- Do not paste JavaScript object literals where JSON is expected.
- Validate JSON files with a linter before committing.
When it happens
Trigger: Parsing an object like `{name: 1}` (unquoted key), `{'name': 1}` (single-quoted key), or a truncated object `{"a":1,` where the parser expects the next key and hits `}` end-of-input.
Common situations: Hand-written JSON copied from JavaScript object literals; template-generated JSON with missing quotes; config files edited by hand and saved with JS-literal syntax.
Understand the failure class
Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.
Related errors
- Unexpected token ' ' in JSON
- Expected ':' after object key
- Unexpected end of JSON input in object
- Unexpected end of JSON input in array
- Invalid number: bare '-'
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/5348ba3e20ce5687.
Report an issue: GitHub.
Appendix: source
Thrown at karate-js/src/main/java/io/karatelabs/js/JsonParser.java:115
return parseNumber();
}
throw syntaxError("Unexpected token '" + c + "' in JSON");
}
}
private Map<String, Object> parseObject() {
// we know s.charAt(pos) == '{'
pos++;
Map<String, Object> map = new LinkedHashMap<>();
skipWs();
if (pos < len && s.charAt(pos) == '}') {
pos++;
return map;
}
while (true) {
skipWs();
if (pos >= len || s.charAt(pos) != '"') {
throw syntaxError("Expected string key in object");
}
String key = parseString();
skipWs();
if (pos >= len || s.charAt(pos) != ':') {
throw syntaxError("Expected ':' after object key");
}
pos++;
skipWs();
Object value = parseValue();
// RFC 8259: behavior of duplicate keys is unspecified; we keep
// the last value (matches json-smart parseKeepingOrder).
map.put(key, value);
skipWs();
if (pos >= len) {
throw syntaxError("Unexpected end of JSON input in object");
}
char c = s.charAt(pos);
if (c == ',') {View on GitHub (pinned to a22eb90246)