SeaQL/sea-orm · error

Failed to get json

Error message

Failed to get json

What it means

A panic from `.expect("Failed to get json")` in ProxyRow's MySQL conversion: the column type_info reports JSON, but `row.try_get::<Option<serde_json::Value>>` failed, so expect aborts instead of returning an error. This requires the with-json feature.

Source

Thrown at sea-orm-sync/src/driver/sqlx_mysql.rs:533

                        "DECIMAL" => Value::BigDecimal(
                            row.try_get::<Option<bigdecimal::BigDecimal>, _>(c.ordinal())
                                .expect("Failed to get decimal")
                                .map(Box::new),
                        ),
                        #[cfg(all(
                            feature = "with-rust_decimal",
                            not(feature = "with-bigdecimal")
                        ))]
                        "DECIMAL" => Value::Decimal(
                            row.try_get::<Option<rust_decimal::Decimal>, _>(c.ordinal())
                                .expect("Failed to get decimal")
                                .map(Box::new),
                        ),

                        #[cfg(feature = "with-json")]
                        "JSON" => Value::Json(
                            row.try_get::<Option<serde_json::Value>, _>(c.ordinal())
                                .expect("Failed to get json")
                                .map(Box::new),
                        ),

                        _ => unreachable!("Unknown column type: {}", c.type_info().name()),
                    },
                )
            })
            .collect(),
    }
}

View on GitHub (pinned to e29bcd1b41)

Solutions

  1. Validate the column contents: run SELECT and JSON_VALID(col) to find malformed rows and repair or NULL them.
  2. Ensure the column is a real MySQL JSON type and your sqlx version decodes JSON natively; cast with CAST(col AS JSON) in the query if needed.
  3. Align sea-orm/sqlx versions so JSON decoding is supported for your MySQL server version.
  4. Change the expect to a mapped error so decode failures return DbErr instead of panicking.

Example fix

// before
row.try_get::<Option<serde_json::Value>, _>(c.ordinal())
    .expect("Failed to get json")
// after
row.try_get::<Option<serde_json::Value>, _>(c.ordinal())
    .map_err(|e| DbErr::Custom(format!("json decode failed for {}: {e}", c.name())))?
Defensive patterns

Strategy: validation

Validate before calling

// Check stored JSON is valid before conversion
// SELECT COUNT(*) FROM t WHERE JSON_VALID(col) = 0;  -- must be 0
let info = c.type_info().name();
if info != "JSON" {
    return Err(format!("unexpected column type {info} for json decoding"));
}

Type guard

fn is_json_column(c: &sqlx::mysql::MySqlColumn) -> bool {
    c.type_info().name() == "JSON"
}

Prevention

When it happens

Trigger: Reading a MySQL JSON column where the raw value is not valid JSON (invalid JSON text stored via casts or older column types), or sqlx decodes it as String rather than serde_json::Value so the type lookup fails.

Common situations: Columns migrated from TEXT/LONGTEXT to JSON containing malformed data; reading JSON columns through drivers/versions that return them as strings; JSON columns containing values sqlx's serde_json decoder rejects.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of SeaQL/sea-orm@e29bcd1b41 (2026-09-10). Data as JSON: /api/errors/640c961d3f0accbc. Report an issue: GitHub.