{"record":{"id":"13df4f182b0a962c","repo":"apple/pkl","slug":"jsonparseerror","errorCode":"jsonParseError","errorMessage":"jsonParseError","messagePattern":"jsonParseError","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/stdlib/json/ParserNodes.java","lineNumber":62,"sourceCode":"    }\n\n    @Specialization\n    @TruffleBoundary\n    protected Object eval(\n        VmTyped self, VmTyped resource, @Cached(\"create()\") IndirectCallNode callNode) {\n      var text = (String) VmUtils.readMember(resource, Identifier.TEXT, callNode);\n      return doParse(self, text);\n    }\n\n    private Object doParse(VmTyped self, String text) {\n      var converter = PklConverter.fromParser(self);\n      var useMapping = (boolean) VmUtils.readMember(self, Identifier.USE_MAPPING);\n      var handler = new Handler(converter, useMapping);\n      var parser = new JsonParser(handler);\n      try {\n        parser.parse(text);\n      } catch (ParseException e) {\n        throw exceptionBuilder().evalError(\"jsonParseError\").withHint(e.getMessage()).build();\n      }\n      return converter.convert(handler.value, List.of(VmValueConverter.TOP_LEVEL_VALUE));\n    }\n  }\n\n  private static class Handler\n      extends JsonHandler<EconomicMap<Object, ObjectMember>, EconomicMap<Object, ObjectMember>> {\n    private final PklConverter converter;\n    private final boolean useMapping;\n\n    private final Deque<Object> currPath = new ArrayDeque<>();\n\n    @LateInit private Object value;\n\n    public Handler(PklConverter converter, boolean useMapping) {\n      this.converter = converter;\n      this.useMapping = useMapping;\n","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/stdlib/json/ParserNodes.java#L44-L80","documentation":"Pkl's `json.parse(...)` throws `jsonParseError` when the built-in JSON parser (org.pkl.core.util.json.JsonParser) rejects the input text as invalid JSON. The underlying ParseException message is attached as a hint, e.g. expected value, unexpected token, or trailing characters. This is Pkl's standard stdlib behavior: any JSON syntax violation in the string handed to `json.parse` becomes an evalError.","triggerScenarios":"Calling `json.parse(text)` where `text` is malformed JSON: truncated input, unquoted or single-quoted keys/strings, trailing commas, JavaScript literals (undefined, NaN), or non-JSON content (HTML, CSV) passed by mistake. Raised in ParserNodes.doParse after JsonParser.parse throws ParseException.","commonSituations":"Reading JSON from a file or HTTP response that is actually an error page or truncated download; hand-edited JSON with a missing comma or quote; passing a Pkl string interpolation result that contains YAML or XML instead of JSON.","solutions":["Validate the JSON text with an external JSON validator or `json.parse` in a scratch script to locate the syntax problem via the error hint","Check that the source file/response you are parsing is complete and actually JSON (correct file, no HTML error page)","Fix the specific syntax issue named in the hint (quote keys, remove trailing commas, replace single quotes)","If the input may be invalid, wrap parsing in your own error handling: decode JSON on the producing side instead of a raw string"],"exampleFix":"// before\ntext: String = read(\"*.json\")\nrender(text)\n// after\nx = json.parse(text)","handlingStrategy":"try-catch","validationCode":"function isValidJson(text: String): Boolean {\n  try { json.parse(text); return true } catch (e) { return false }\n}","typeGuard":null,"tryCatchPattern":"try {\n  value = json.parse(text)\n} catch (e: PklException) {\n  trace(\"JSON parse failed: ${e.message}\")\n  // fallback or rethrow with context\n}","preventionTips":["Lint JSON files before committing (pre-commit jsonlint hook)","Catch empty bodies and HTML error pages before parsing","Prefer reading structured formats (Pkl/YAML) natively instead of round-tripping through JSON strings"],"tags":["json","pkl","stdlib","parsing"],"backgroundTag":"json-parse-error","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}