{"record":{"id":"f59debe4c41edb3b","repo":"apple/pkl","slug":"missingfieldexception-this-key","errorCode":null,"errorMessage":"MissingFieldException(this, key)","messagePattern":"MissingFieldException\\(this, key\\)","errorType":"exception","errorClass":"MappingException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/util/json/Json.java","lineNumber":239,"sourceCode":"    private final Map<String, @Nullable Object> delegate;\n\n    public JsObject() {\n      this.delegate = new HashMap<>();\n    }\n\n    public JsObject(int size) {\n      this.delegate = new HashMap<>(size);\n    }\n\n    public <T> T get(String key, Mapper<T> mapper) throws JsonParseException {\n      var ret = get(key);\n      if (ret == null) {\n        throw new MissingFieldException(this, key);\n      }\n      try {\n        return mapper.apply(ret);\n      } catch (Exception e) {\n        throw new MappingException(key, e);\n      }\n    }\n\n    public <T> @Nullable T getNullable(String key, Mapper<T> mapper) throws JsonParseException {\n      var ret = get(key);\n      if (ret == null) {\n        return null;\n      }\n      return get(key, mapper);\n    }\n\n    public boolean getBoolean(String key) throws JsonParseException {\n      var ret = get(key);\n      if (ret == null) {\n        throw new MissingFieldException(this, key);\n      }\n      if (!(ret instanceof Boolean b)) {\n        throw new FormatException(key, \"boolean\", ret.getClass());","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/util/json/Json.java#L221-L257","documentation":"JsObject.get(key, mapper) throws MissingFieldException when the key is absent from the JSON object (or maps to JSON null), because the mapper result would be indistinguishable from null. Unlike getNullable, get treats a missing key as a hard error.","triggerScenarios":"Calling get(key, mapper) or getNullable on a JSON object where the key does not exist, e.g. parsing a response and reading obj.get(\"id\", ...) when the server omitted \"id\".","commonSituations":"Optional fields in API responses absent in error/degraded responses; schema drift after API version changes; fields only present in newer payload versions.","solutions":["Use getNullable(key, mapper) when the field is genuinely optional.","Check key presence first (containsKey or inspecting the map) before calling get.","Fix the producer/schema so the field is always emitted."],"exampleFix":"// before\nString id = obj.get(\"id\", JsString::value);\n// after\nString id = obj.getNullable(\"id\", JsString::value);\nif (id == null) { /* handle missing */ }","handlingStrategy":"type-guard","validationCode":"if (obj.getNullable(key, Function.identity()) == null) { /* default or fail fast with a clear message */ }","typeGuard":"String id = obj.getNullable(\"id\", JsString::value);\nif (id == null) throw new IllegalStateException(\"missing required field 'id' in payload\");","tryCatchPattern":"try {\n  String id = obj.get(\"id\", JsString::value);\n} catch (Json.MissingFieldException e) {\n  // supply default or report missing field\n}","preventionTips":["Use getNullable for optional fields; reserve get for required ones.","Validate payloads against a schema before extraction.","Check API version compatibility when fields appear/disappear."],"tags":["json","missing-field","parsing"],"backgroundTag":"missing-required-config-field","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}