karatelabs/karate · error
Expected ':' after object key
Error message
Expected ':' after object key
What it means
After reading an object key, parseObject requires a `:` separator before the value. Missing separators, stray characters (e.g. `=` as in JS `var x = {...}` fragments), or a truncated key/value pair cause "Expected ':' after object key".
Solutions
- Insert the missing `:` between key and value in the JSON text.
- Generate JSON programmatically (serializer/library) instead of string concatenation.
- Check for characters like `=` or `=>` that replaced the colon and fix them.
Example fix
// before
var x = JSON.parse('{"a" 1}');
// after
var x = JSON.parse('{"a": 1}'); Defensive patterns
Strategy: validation
Validate before calling
// detect missing colon between key and value: "key" followed by non-colon
if (/"\s*[^\s:]\s*[,}\d]/.test(input) && !/"\s*:/.test(input)) {
throw new IllegalArgumentException('each key needs a colon before its value');
} Try / catch
try {
var obj = JSON.parse(input);
} catch (e) {
if (('' + e).indexOf("Expected ':' after object key") >= 0) {
console.log('a colon is missing between a key and its value');
}
throw e;
} Prevention
- Build JSON with a serializer instead of string concatenation.
- After editing JSON by hand, re-check every `key value` pair for its colon.
- Use an editor with JSON syntax highlighting to catch missing separators.
When it happens
Trigger: Parsing `{"a" 1}`, `{"a" = 1}`, or input where whitespace/newline corruption removed the colon between key and value; nested parseValue calls then re-enter parseObject and fail at the separator check.
Common situations: Editing JSON by hand and deleting a colon; string manipulation that splits/mangles key-value pairs; generating JSON via string concatenation instead of a serializer.
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 string key in object
- 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/a415bcf781de0901.
Report an issue: GitHub.
Appendix: source
Thrown at karate-js/src/main/java/io/karatelabs/js/JsonParser.java:120
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 == ',') {
pos++;
// trailing-comma is invalid per spec: the next iteration
// demands a string key, which rejects e.g. {"a":1,}.
continue;
}View on GitHub (pinned to a22eb90246)