SeaQL/sea-orm · critical
Failed to get json
Error message
Failed to get json
What it means
This panic occurs when sea-orm's Postgres driver cannot decode a JSON/JSONB column into Option<serde_json::Value> (requires `with-json`). try_get fails when the cell is not actually json/jsonb on the wire (e.g. it is text), or the sqlx build lacks json support so the decode is rejected. Because the code uses `.expect`, the failure is a panic instead of a returned error.
Source
Thrown at sea-orm-sync/src/driver/sqlx_postgres.rs:643
}
#[cfg(feature = "postgres-array")]
"OID[]" => Value::Array(
sea_query::ArrayType::BigInt,
row.try_get::<Option<Vec<i64>>, _>(c.ordinal())
.expect("Failed to get oid array")
.map(|vals| {
Box::new(
vals.into_iter()
.map(|val| Value::BigInt(Some(val)))
.collect(),
)
}),
),
#[cfg(feature = "with-json")]
"JSON" | "JSONB" => Value::Json(
row.try_get::<Option<serde_json::Value>, _>(c.ordinal())
.expect("Failed to get json")
.map(Box::new),
),
#[cfg(all(
feature = "with-json",
any(feature = "json-array", feature = "postgres-array")
))]
"JSON[]" | "JSONB[]" => Value::Array(
sea_query::ArrayType::Json,
row.try_get::<Option<Vec<serde_json::Value>>, _>(c.ordinal())
.expect("Failed to get json array")
.map(|vals| {
Box::new(
vals.into_iter()
.map(|val| Value::Json(Some(Box::new(val))))
.collect(),
)
}),
),View on GitHub (pinned to e29bcd1b41)
Solutions
- Ensure the `with-json` feature is enabled consistently on sea-orm and that the sqlx version it pulls supports json decoding.
- Verify the column type with `SELECT pg_typeof(col)`; cast explicitly if a query returns text: `col::jsonb`.
- Avoid UNION/CASE mixes that downgrade jsonb to text; cast each branch to jsonb.
- Run `cargo tree -i sqlx` to confirm a single sqlx version with the json feature is linked.
Example fix
// before SELECT CASE WHEN ok THEN data ELSE 'null' END AS data FROM t; // after SELECT CASE WHEN ok THEN data ELSE 'null'::jsonb END::jsonb AS data FROM t;
Defensive patterns
Strategy: validation
Validate before calling
// Check that the JSON feature set is coherent and the column is really json/jsonb:
let t = db.query_one(Statement::from_string(
DatabaseBackend::Postgres,
"SELECT pg_typeof(data)::text FROM t LIMIT 1",
)).await?;
// expect "json" or "jsonb" Try / catch
// Since this is a panic, avoid it by decoding nullable-json via raw SQL with an explicit cast:
let rows = db.query_all(Statement::from_string(
DatabaseBackend::Postgres,
"SELECT data::jsonb FROM t",
)).await?;
// then try_get::<Option<serde_json::Value>, _>(0)? returns a Result you can handle. Prevention
- Enable with-json on sea-orm in every workspace crate so a single coherent sqlx build is linked.
- Run `cargo tree -d` to detect duplicate sqlx versions with different features.
- Cast CASE/UNION branches to jsonb to prevent type downgrade to text.
- Declare entity columns as Json only when the DB column is json/jsonb.
When it happens
Trigger: Selecting a JSON/JSONB column via raw SQL where the driver returns the type under a different name (e.g. from a view or a `to_jsonb(...)` in a context that yields text), or with the `with-json` feature disabled in the sqlx dependency while sea-orm compiled the JSON branch, or deserializing from a connection built with mismatched feature sets.
Common situations: Feature-flag drift between sea-orm and sqlx in the workspace (one crate enables with-json, another doesn't); reading JSON columns through UNION/CASE expressions where Postgres infers text; older sqlx versions with jsonb decoding bugs.
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
- Failed to get json array
- Failed to get numeric array
- Failed to get oid
- Failed to get oid array
- Failed to get ip address
AI-assisted analysis of SeaQL/sea-orm@e29bcd1b41 (2026-09-10).
Data as JSON: /api/errors/61312b9c0bc52b72.
Report an issue: GitHub.