{"record":{"id":"3f109b3e7193d88d","repo":"spring-projects/spring-ai","slug":"failed-to-parse-json-3f109b","errorCode":null,"errorMessage":"Failed to parse JSON: ","messagePattern":"Failed to parse JSON: ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"models/spring-ai-google-genai/src/main/java/org/springframework/ai/google/genai/schema/JsonSchemaConverter.java","lineNumber":54,"sourceCode":" */\npublic final class JsonSchemaConverter {\n\n\tprivate JsonSchemaConverter() {\n\t\t// Prevent instantiation\n\t}\n\n\t/**\n\t * Parses a JSON string into an ObjectNode.\n\t * @param jsonString The JSON string to parse\n\t * @return ObjectNode containing the parsed JSON\n\t * @throws RuntimeException if the JSON string cannot be parsed\n\t */\n\tpublic static ObjectNode fromJson(String jsonString) {\n\t\ttry {\n\t\t\treturn (ObjectNode) JacksonUtils.getDefaultJsonMapper().readTree(jsonString);\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\tthrow new RuntimeException(\"Failed to parse JSON: \" + jsonString, e);\n\t\t}\n\t}\n\n\t/**\n\t * Converts a JSON Schema ObjectNode to OpenAPI schema format.\n\t * @param jsonSchemaNode The input JSON Schema as ObjectNode\n\t * @return ObjectNode containing the OpenAPI schema\n\t * @throws IllegalArgumentException if jsonSchemaNode is null\n\t */\n\tpublic static ObjectNode convertToOpenApiSchema(ObjectNode jsonSchemaNode) {\n\t\tAssert.notNull(jsonSchemaNode, \"JSON Schema node must not be null\");\n\t\tAssert.isTrue(!jsonSchemaNode.has(\"$defs\"), \"Google's Structured Output schema doesn't support $defs property\");\n\n\t\ttry {\n\t\t\t// Convert to OpenAPI schema using our custom conversion logic\n\t\t\tObjectNode openApiSchema = convertSchema(jsonSchemaNode,\n\t\t\t\t\tJacksonUtils.getDefaultJsonMapper().getNodeFactory());\n","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/models/spring-ai-google-genai/src/main/java/org/springframework/ai/google/genai/schema/JsonSchemaConverter.java#L36-L72","documentation":"JsonSchemaConverter.fromJson(jsonString) parses a JSON string into a Jackson ObjectNode using the shared mapper; any parse/IO exception is rethrown as a RuntimeException with the offending string in the message. The library throws this when the supplied text is not valid JSON.","triggerScenarios":"Calling fromJson with malformed JSON, empty/blank input, or JSON that fails to read (e.g., trailing garbage, unquoted keys).","commonSituations":"Hand-written JSON Schemas pasted into code, schemas read from files/LLM output with stray text, or truncation of large schema strings.","solutions":["Validate the JSON string with a linter/parser before passing it in","Trim whitespace and remove non-JSON prefixes/suffixes (e.g., markdown fences from LLM output)","Confirm the string is not empty or truncated","Catch RuntimeException and surface the failing input for debugging"],"exampleFix":"// before\nObjectNode schema = JsonSchemaConverter.fromJson(raw);\n// after\nString cleaned = raw.strip().replaceAll(\"^```(json)?|```$\", \"\");\nObjectNode schema = JsonSchemaConverter.fromJson(cleaned);","handlingStrategy":"validation","validationCode":"if (json == null || json.isBlank()) {\n    throw new IllegalArgumentException(\"empty JSON input\");\n}\ntry (var p = new java.io.StringReader(json)) {\n    new com.fasterxml.jackson.core.JsonFactory().createParser(p).close(); // cheap syntax probe\n}","typeGuard":"boolean isProbablyJson(String s) {\n    if (s == null) return false;\n    String t = s.strip();\n    return (t.startsWith(\"{\") && t.endsWith(\"}\")) || (t.startsWith(\"[\") && t.endsWith(\"]\"));\n}","tryCatchPattern":"try {\n    ObjectNode node = JsonSchemaConverter.fromJson(json);\n} catch (RuntimeException e) {\n    logger.error(\"bad schema input: {}\", e.getMessage());\n    throw new IllegalArgumentException(\"schema must be valid JSON\", e);\n}","preventionTips":["Strip markdown code fences from LLM-emitted schemas","Lint JSON schemas in CI before shipping them","Check inputs are not empty or truncated","Keep schemas as resource files and validate at build time"],"tags":["json","parsing","schema","jackson"],"backgroundTag":"json-parse-error","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}