{"record":{"id":"a6f93ba9cfa773c9","repo":"apache/seatunnel","slug":"failed-to-convert-row-to-json","errorCode":null,"errorMessage":"Failed to convert row to JSON","messagePattern":"Failed to convert row to JSON","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"seatunnel-connectors-v2/connector-databend/src/main/java/org/apache/seatunnel/connectors/seatunnel/databend/sink/DatabendSinkWriter.java","lineNumber":523,"sourceCode":"                    jsonNode.put(fieldName, (Double) value);\n                } else if (value instanceof Boolean) {\n                    jsonNode.put(fieldName, (Boolean) value);\n                } else if (value instanceof BigDecimal) {\n                    jsonNode.put(fieldName, (BigDecimal) value);\n                } else if (value instanceof java.sql.Timestamp) {\n                    jsonNode.put(fieldName, value.toString());\n                } else if (value instanceof java.sql.Date) {\n                    jsonNode.put(fieldName, value.toString());\n                } else if (value instanceof byte[]) {\n                    jsonNode.put(fieldName, Base64.getEncoder().encodeToString((byte[]) value));\n                } else {\n                    jsonNode.put(fieldName, value.toString());\n                }\n            }\n\n            return objectMapper.writeValueAsString(jsonNode);\n        } catch (Exception e) {\n            throw new RuntimeException(\"Failed to convert row to JSON\", e);\n        }\n    }\n\n    private void initializePreparedStatement(SeaTunnelRow row) throws SQLException {\n        log.info(\"Initializing PreparedStatement based on row data\");\n\n        // use sinkTablePath to get Schema\n        String database = sinkTablePath.getDatabaseName();\n        String table = sinkTablePath.getTableName();\n\n        log.info(\"Querying target table schema for {}.{}\", database, table);\n        SeaTunnelRowType actualTableSchema = queryTableSchema(database, table);\n\n        if (actualTableSchema != null) {\n            log.info(\"Using actual table schema: {}\", actualTableSchema);\n            this.insertSql = generateInsertSql(database, table, actualTableSchema);\n        } else {\n            log.warn(\"Could not query table schema, using inferred schema from data\");","sourceCodeStart":505,"sourceCodeEnd":541,"githubUrl":"https://github.com/apache/seatunnel/blob/cf67b549a7a6c35fa0beb12d83c62892427ea919/seatunnel-connectors-v2/connector-databend/src/main/java/org/apache/seatunnel/connectors/seatunnel/databend/sink/DatabendSinkWriter.java#L505-L541","documentation":"convertRowToJson serializes a SeaTunnelRow into a JSON string (used for JSON-based writes). Any exception during field mapping or Jackson serialization is rethrown as a RuntimeException with this message, chaining the original cause. It signals a row that cannot be represented as the expected JSON document.","triggerScenarios":"objectMapper.writeValueAsString throws (unsupported value types, Jackson config issue), or row fields contain objects that fail toString/field mapping; also triggered when the row shape does not match the expected catalog metadata during field iteration.","commonSituations":"Binary/special types (e.g. byte arrays, nested arrays) that Jackson cannot map by default; schema drift so row fields no longer match declared catalog columns; null catalog metadata causing wrong field-name lookups; corrupt values from upstream CDC sources.","solutions":["Read the chained cause ('Caused by') to see the exact serialization failure.","Check for unsupported SeaTunnel types in the row and map them to JSON-compatible types with an upstream transform.","Verify the catalog table metadata is loaded correctly so field names align with row fields.","Cast/normalize special columns (binary, complex types) before the Databend sink."],"exampleFix":"// before: raw binary field reaches JSON conversion\nrow.setField(2, bytes);\n// after: encode binary before sinking\nrow.setField(2, Base64.getEncoder().encodeToString(bytes));","handlingStrategy":"type-guard","validationCode":"// ensure every field is JSON-safe before sinking\nfor (int i = 0; i < row.getArity(); i++) {\n    Object v = row.getField(i);\n    if (v instanceof byte[] || v instanceof Map || v instanceof List) {\n        // normalize before write\n    }\n}","typeGuard":"boolean isJsonSerializable(Object v) {\n    return v == null || v instanceof String || v instanceof Number\n        || v instanceof Boolean || v instanceof java.time.temporal.Temporal;\n}","tryCatchPattern":"try {\n    writer.write(row);\n} catch (RuntimeException e) {\n    if (\"Failed to convert row to JSON\".equals(e.getMessage())) {\n        log.error(\"JSON conversion failed; offending row types: \"\n            + Arrays.toString(row.getRowType().getFieldTypes()), e.getCause());\n    } else throw e;\n}","preventionTips":["Encode binary/special types as strings upstream","Keep row schema in sync with catalog metadata","Test serialization with representative production rows","Pin a compatible Jackson version in the deployment"],"tags":["databend","json","serialization","sink"],"backgroundTag":"json-serialization-failed","analyzedSha":"cf67b549a7a6c35fa0beb12d83c62892427ea919","analyzedAt":"2026-09-10T21:44:55.265Z","contentChangedAt":"2026-09-10T21:44:55.265Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}