Skip to main content
Version: 0.11.x

JSON

Select JSON Result

All SeaORM selects are capable of returning serde_json::Value.

// Find by id
let cake: Option<serde_json::Value> = Cake::find_by_id(1)
.into_json()
.one(db)
.await?;

assert_eq!(
cake,
Some(serde_json::json!({
"id": 1,
"name": "Cheese Cake"
}))
);

// Find with filter
let cakes: Vec<serde_json::Value> = Cake::find()
.filter(cake::Column::Name.contains("chocolate"))
.order_by_asc(cake::Column::Name)
.into_json()
.all(db)
.await?;

assert_eq!(
cakes,
[
serde_json::json!({
"id": 2,
"name": "Chocolate Forest"
}),
serde_json::json!({
"id": 8,
"name": "Chocolate Cupcake"
}),
]
);

// Paginate json result
let cake_pages: Paginator<_> = Cake::find()
.filter(cake::Column::Name.contains("chocolate"))
.order_by_asc(cake::Column::Name)
.into_json()
.paginate(db, 50);

while let Some(cakes) = cake_pages.fetch_and_next().await? {
// Do something on cakes: Vec<serde_json::Value>
}