{"record":{"id":"f7d8fe851d38041e","repo":"json-path/JsonPath","slug":"document-contains-the-path-s-but-was-expected-n","errorCode":null,"errorMessage":"Document contains the path <%s> but was expected not to.","messagePattern":"Document contains the path <(.+?)> but was expected not to\\.","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java","lineNumber":76,"sourceCode":"    }\n\n    /**\n     * {@inheritDoc}\n     */\n    public <T> JsonAsserter assertEquals(String path, T expected) {\n        return assertThat(path, equalTo(expected));\n    }\n\n    /**\n     * {@inheritDoc}\n     */\n    public JsonAsserter assertNotDefined(String path) {\n\n        try {\n            Configuration c = Configuration.defaultConfiguration();\n\n            JsonPath.using(c).parse(jsonObject).read(path);\n            throw new AssertionError(format(\"Document contains the path <%s> but was expected not to.\", path));\n        } catch (PathNotFoundException e) {\n        }\n        return this;\n    }\n\n    @Override\n    public JsonAsserter assertNotDefined(String path, String message) {\n        try {\n            Configuration c = Configuration.defaultConfiguration();\n\n            JsonPath.using(c).parse(jsonObject).read(path);\n\n            throw new AssertionError(format(\"Document contains the path <%s> but was expected not to.\", path));\n        } catch (PathNotFoundException e) {\n        }\n        return this;\n    }\n","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java#L58-L94","documentation":"assertNotDefined verifies that a JSON path does NOT exist in the document. It attempts to read the path; if the read succeeds (PathNotFoundException is NOT thrown), the path is defined and an AssertionError is thrown. This is an intentional negative-existence assertion failure.","triggerScenarios":"JsonAssert.with(json).assertNotDefined(\"$.removedField\") when the field is still present in the document; also invoked by the two-arg overload with a message.","commonSituations":"Regression tests asserting a deprecated/removed field is gone from an API response, or that a filter matched nothing — failing because the field was re-added or the path actually resolves.","solutions":["Inspect the JSON document at that path to confirm the field still exists.","Remove the field at the source (fixture, API, serialization config) if it should be gone.","If presence is now legitimate, delete or invert the assertion.","Double-check path syntax — a too-broad path like '$..name' can unexpectedly resolve."],"exampleFix":"// before\nwith(json).assertNotDefined(\"$.legacyId\");\n// after\nif (JsonPath.read(json, \"$.legacyId\") != null) {\n    json.remove(\"legacyId\"); // or update fixture\n}\nwith(json).assertNotDefined(\"$.legacyId\");","handlingStrategy":"validation","validationCode":"try { JsonPath.read(json, path); failOnce(path + \" is defined\"); } catch (PathNotFoundException expected) { /* ok */ }","typeGuard":null,"tryCatchPattern":"try {\n    with(json).assertNotDefined(path);\n} catch (AssertionError e) {\n    log.warn(\"Unexpectedly defined: {}\", path);\n    throw e;\n}","preventionTips":["After removing a field, grep the serialization code/fixture for it.","Use precise paths rather than broad recursive-descent (..) ones.","Run this assertion in CI against freshly generated responses."],"tags":["json","assertion","testing","path-not-exists"],"backgroundTag":"schema-validation-failed","analyzedSha":"62a4c9f0f65ba3f625aa0867d64c528ba72d09ec","analyzedAt":"2026-09-11T11:36:00.448Z","contentChangedAt":"2026-09-11T11:36:00.448Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}