{"record":{"id":"4e9399695a27339e","repo":"prestodb/presto","slug":"invalid-function-argument-4e9399","errorCode":"INVALID_FUNCTION_ARGUMENT","errorMessage":"Cannot convert '%s' to JSON","messagePattern":"Cannot convert '(.+?)' to JSON","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-postgresql/src/main/java/com/facebook/presto/plugin/postgresql/PostgreSqlClient.java","lineNumber":219,"sourceCode":"    {\n        return createSliceReadMapping(\n                jsonType,\n                (resultSet, columnIndex) -> jsonParse(utf8Slice(resultSet.getString(columnIndex))));\n    }\n\n    public static Slice jsonParse(Slice slice)\n    {\n        try (JsonParser parser = createJsonParser(JSON_FACTORY, slice)) {\n            byte[] in = slice.getBytes();\n            SliceOutput dynamicSliceOutput = new DynamicSliceOutput(in.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 (Exception e) {\n            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format(\"Cannot convert '%s' to JSON\", slice.toStringUtf8()));\n        }\n    }\n\n    public static JsonParser createJsonParser(JsonFactory factory, Slice json)\n            throws IOException\n    {\n        // Jackson tries to detect the character encoding automatically when using InputStream\n        // so we pass an InputStreamReader instead.\n        return factory.createParser(new InputStreamReader(json.getInput(), UTF_8));\n    }\n\n    private ReadMapping uuidReadMapping()\n    {\n        return createSliceReadMapping(\n                uuidType,\n                (resultSet, columnIndex) -> uuidSlice((UUID) resultSet.getObject(columnIndex)));\n    }\n","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-postgresql/src/main/java/com/facebook/presto/plugin/postgresql/PostgreSqlClient.java#L201-L237","documentation":"jsonReadMapping converts PostgreSQL json/jsonb values from the wire format into Presto JSON slices using a Jackson streaming parser. If any exception occurs while parsing/rewriting the value (including trailing characters after a valid JSON document), jsonParse throws a PrestoException with function code INVALID_FUNCTION_ARGUMENT naming the offending bytes. The original exception is deliberately not chained, only its message is surfaced via the value itself.","triggerScenarios":"Reading a json/jsonb column whose value Jackson cannot parse in the expected format — corrupt/malformed JSON stored by a non-Presto writer, invalid UTF-8 bytes, or a value written with extensions Jackson's configured JsonFactory rejects.","commonSituations":"Data inserted into PostgreSQL from applications that stored invalid JSON in text columns cast to json, encoding mismatches (latin1 vs UTF-8) corrupting multi-byte characters, or manually edited rows with trailing garbage.","solutions":["Locate the offending row: SELECT id FROM t WHERE json_typeof(col) IS NULL or validate values with col::text::jsonb","Fix/clean the stored JSON data in PostgreSQL (re-insert valid JSON)","Cast the column to text and read it as VARCHAR instead of JSON if the values are not guaranteed valid","Check client encoding settings on the writing application (UTF-8 end-to-end)"],"exampleFix":"// before\nSELECT json_col FROM t;  -- fails on malformed row\n// after\nSELECT json_col::text AS json_col FROM t WHERE jsonb_typeof(json_col) = 'object';","handlingStrategy":"validation","validationCode":"sql\nSELECT id FROM my_table\nWHERE jsonb_typeof(col::jsonb) IS NULL OR col::text NOT LIKE '{%';\n-- rows returned hold invalid JSON","typeGuard":null,"tryCatchPattern":"java\ntry (ResultSet rs = stmt.executeQuery(\"SELECT json_col FROM t\")) {\n    while (rs.next()) {\n        try {\n            consume(rs.getObject(\"json_col\"));\n        } catch (PrestoException e) {\n            if (e.getErrorCode() == INVALID_FUNCTION_ARGUMENT.toErrorCode().getCode()) {\n                log.warn(\"Skipping invalid JSON row\");\n                continue;\n            }\n            throw e;\n        }\n    }\n}","preventionTips":["Use jsonb columns with constrained inserts so PostgreSQL rejects invalid JSON at write time","Validate JSON in the writing application before insert","Enforce UTF-8 encoding end-to-end","Schedule data-quality checks scanning json columns for parse failures"],"tags":["postgresql","json","invalid-argument","data-corruption"],"backgroundTag":"invalid-json","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"}