{"record":{"id":"28c2e9ad034e24be","repo":"karatelabs/karate","slug":"cannot-replace-root-path","errorCode":null,"errorMessage":"cannot replace root path $","messagePattern":"cannot replace root path \\$","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/common/Json.java","lineNumber":305,"sourceCode":"        if (leftPos == -1) {\n            return -1;\n        }\n        int rightPos = s.indexOf(']', leftPos);\n        String num = s.substring(leftPos + 1, rightPos);\n        if (num.isEmpty()) {\n            return -1;\n        }\n        try {\n            return Integer.parseInt(num);\n        } catch (NumberFormatException e) {\n            return -1;\n        }\n    }\n\n    private void setInternal(String path, Object o) {\n        path = prefix(path);\n        if (\"$\".equals(path)) {\n            throw new RuntimeException(\"cannot replace root path $\");\n        }\n        boolean forArray = isArrayPath(path);\n        if (!pathExists(path)) {\n            createPath(path, forArray);\n        }\n        Pair<String> pair = toParentAndLeaf(path);\n        if (forArray) {\n            int index = arrayIndex(pair.right);\n            if (index == -1) {\n                doc.add(arrayKey(path), o);\n            } else {\n                doc.set(path, o);\n            }\n        } else {\n            doc.put(pair.left, pair.right, o);\n        }\n    }\n","sourceCodeStart":287,"sourceCodeEnd":323,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/common/Json.java#L287-L323","documentation":"Json.setInternal() implements Json.set()/setAsString(); replacing the entire root ($, i.e. the whole document) is not supported because the document context was built around the existing root value. Callers must instead mutate paths inside the document.","triggerScenarios":"json.set(\"$\", newMap) or json.set(\"$\", value), or setAsString(\"$\", jsonText) — targeting the root path.","commonSituations":"Wanting to wholesale-replace a parsed document's content; porting code that assumed set('$') acts like assignment.","solutions":["Set child paths instead, e.g. set(\"$.field\", value)","Build a new Json from the desired value via Json.of(newMap) and use that object","Use document mutation helpers or re-create the Json wrapper"],"exampleFix":"// before\njson.set(\"$\", newMap);\n// after\njson.set(\"$.field\", newMap.get(\"field\")); // or: json = Json.of(newMap);","handlingStrategy":"validation","validationCode":"if (\"$\".equals(path) || path == null) { throw new IllegalArgumentException(\"set() cannot replace root; use Json.of() to build a new document\"); }","typeGuard":null,"tryCatchPattern":"try { json.set(path, value); } catch (RuntimeException e) { if (\"cannot replace root path $\".equals(e.getMessage())) { json = Json.of(value); } else { throw e; } }","preventionTips":["Never pass \"$\" or empty path to Json.set; set child paths","Rebuild the document with Json.of(newMap) for wholesale replacement","Wrap path writes in a helper that rejects root paths up front"],"tags":["java","karate","json-path","unsupported"],"backgroundTag":"unsupported-operation","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}