{"record":{"id":"465f70389dcfa4af","repo":"prestodb/presto","slug":"invalid-function-argument-465f70","errorCode":"INVALID_FUNCTION_ARGUMENT","errorMessage":"Cannot convert value to JSON: '%s'","messagePattern":"Cannot convert value to JSON: '(.+?)'","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-plugin-toolkit/src/main/java/com/facebook/presto/plugin/base/JsonTypeUtil.java","lineNumber":57,"sourceCode":"    private static final ObjectMapper SORTED_MAPPER = new JsonObjectMapperProvider().get().configure(ORDER_MAP_ENTRIES_BY_KEYS, true);\n\n    private JsonTypeUtil() {}\n\n    public static Slice jsonParse(Slice slice)\n    {\n        // cast(json_parse(x) AS t)` will be optimized into `$internal$json_string_to_array/map/row_cast` in ExpressionOptimizer\n        // If you make changes to this function (e.g. use parse JSON string into some internal representation),\n        // make sure `$internal$json_string_to_array/map/row_cast` is changed accordingly.\n        try (JsonParser parser = createJsonParser(JSON_FACTORY, slice)) {\n            SliceOutput dynamicSliceOutput = new DynamicSliceOutput(slice.length());\n            SORTED_MAPPER.writeValue((OutputStream) dynamicSliceOutput, SORTED_MAPPER.readValue(parser, Object.class));\n            // nextToken() returns null if the input is parsed correctly,\n            // but will throw an exception if there are trailing characters.\n            parser.nextToken();\n            return dynamicSliceOutput.slice();\n        }\n        catch (IOException | RuntimeException e) {\n            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format(\"Cannot convert value to JSON: '%s'\", slice.toStringUtf8()), e);\n        }\n    }\n    private static JsonParser createJsonParser(JsonFactory factory, Slice json)\n            throws IOException\n    {\n        // Jackson tries to detect the character encoding automatically when\n        // using InputStream, so we pass an InputStreamReader instead.\n        return factory.createParser(new InputStreamReader(json.getInput(), UTF_8));\n    }\n}\n","sourceCodeStart":39,"sourceCodeEnd":68,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-plugin-toolkit/src/main/java/com/facebook/presto/plugin/base/JsonTypeUtil.java#L39-L68","documentation":"jsonParse validates that the input slice is well-formed JSON by parsing it with Jackson; any parse failure (malformed JSON, bad encoding, trailing characters) is rethrown as a PrestoException with code INVALID_FUNCTION_ARGUMENT. This matches Presto's convention for JSON functions receiving invalid user input.","triggerScenarios":"Calling a JSON function (e.g. json_parse / json_extract on a varchar) whose argument is not valid JSON: missing quotes around strings, unescaped characters, empty string, concatenated JSON values, or binary garbage in a varbinary-derived slice.","commonSituations":"Columns storing half-written or truncated JSON; CSV imports where JSON fields were not escaped; upstream systems writing Python-style dicts with single quotes instead of valid JSON.","solutions":["Fix the source data so the value is valid JSON (proper quoting, no single quotes, no trailing commas)","Guard the call with a TRY clause: TRY(json_parse(col)) to get NULL instead of a query failure","Pre-validate with json_format/json_extract or a JSON validity check before parsing","Check character encoding — the slice must be UTF-8 text"],"exampleFix":"// before\nSELECT json_parse(raw) FROM events;\n// after\nSELECT TRY(json_parse(raw)) FROM events; -- NULL for invalid rows instead of query failure","handlingStrategy":"validation","validationCode":"-- SQL-level guard\nSELECT CASE WHEN json_valid(raw) THEN json_parse(raw) END FROM t;\n-- or\nSELECT TRY(json_parse(raw)) FROM t;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Wrap untrusted JSON parsing in TRY()","Validate upstream data writes emit RFC 8259 JSON","Reject invalid rows at ingestion time rather than query time"],"tags":["presto","json","invalid-argument"],"backgroundTag":"invalid-json-input","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}