{"record":{"id":"48aae930811410b1","repo":"apache/beam","slug":"failed-to-convert-the-row-to-json-tablecontainer","errorCode":null,"errorMessage":"Failed to convert the row to JSON","messagePattern":"Failed to convert the row to JSON","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/testing/TableContainer.java","lineNumber":131,"sourceCode":"    if (primaryKey != null && !primaryKey.isEmpty()) {\n      if (keyedRows.putIfAbsent(primaryKey, row) != null) {\n        throw new RuntimeException(\n            \"Primary key validation error! Multiple inserts with the same primary key.\");\n      }\n    } else {\n      rows.add(row);\n      if (id != null) {\n        ids.add(id);\n      }\n    }\n\n    long tableSize = table.getNumBytes() == null ? 0L : table.getNumBytes();\n    try {\n      long rowSize = TableRowJsonCoder.of().getEncodedElementByteSize(row);\n      table.setNumBytes(tableSize + rowSize);\n      return rowSize;\n    } catch (Exception ex) {\n      throw new RuntimeException(\"Failed to convert the row to JSON\", ex);\n    }\n  }\n\n  void upsertRow(TableRow row, long sequenceNumber) {\n    List<Object> primaryKey = getPrimaryKey(row);\n    if (primaryKey == null) {\n      throw new RuntimeException(\"Upserts only allowed when using primary keys\");\n    }\n    long lastSequenceNumberForKey = lastSequenceNumber.getOrDefault(primaryKey, Long.MIN_VALUE);\n    if (sequenceNumber <= lastSequenceNumberForKey) {\n      // Out-of-order upsert - ignore it as we've already seen a more-recent update.\n      return;\n    }\n\n    TableRow oldValue = keyedRows.put(primaryKey, row);\n    try {\n      long tableSize = table.getNumBytes() == null ? 0L : table.getNumBytes();\n      if (oldValue != null) {","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/testing/TableContainer.java#L113-L149","documentation":"TableContainer tracks fake table size by encoding each TableRow with TableRowJsonCoder; if computing the encoded size fails for any reason the row cannot be represented as JSON and this RuntimeException wraps the cause. This mirrors size accounting in the fake BigQuery service.","triggerScenarios":"addRow invoked (via insertAll) with a TableRow that TableRowJsonCoder cannot encode — e.g. non-JSON-serializable values or a malformed/nested value type.","commonSituations":"Tests writing rows containing unsupported Java types (e.g. byte arrays, custom POJOs, BigDecimal variants) into the fake BigQuery table.","solutions":["Inspect the cause exception to find the non-encodable field","Convert row values to JSON-safe types (String, Long, Double, Boolean, nested Map/List) before insertAll","Compare against real BigQuery behavior — validate the row with TableRowJsonCoder.of().encodeToString(row) in a unit test"],"exampleFix":"// before\nrow.put(\"payload\", myByteArray);\n// after\nrow.put(\"payload\", Base64.getEncoder().encodeToString(myByteArray));","handlingStrategy":"validation","validationCode":"TableRowJsonCoder.of().getEncodedElementByteSize(row); // throws early if not encodable","typeGuard":null,"tryCatchPattern":"try {\n  container.insertAll(rows);\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"Failed to convert the row to JSON\")) {\n    // inspect row values, fix types, retry\n  } else throw e;\n}","preventionTips":["Restrict row values to JSON-safe types (String, Long, Double, Boolean, Map, List)","Validate sample rows with TableRowJsonCoder in unit tests","Avoid raw byte arrays and exotic numerics in TableRow"],"tags":["java","bigquery","testing","json","serialization"],"backgroundTag":"json-marshal-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}