Skip to content

Convert the result of sqlx::query! to JSON #795

@omikuji

Description

@omikuji

Hi!

I want convert the result of sqlx::query! to JSON.

My ideals are as follows. This one doesn't work.

let examples = sqlx::query!(
    "SELECT id, name FROM example",
)
.fetch_all(&**pool)
.await?;

let json = serde_json::to_string(&examples);

alternative means

#[derive(sqlx::FromRow, serde::Serialize)]
struct Example {
    id: i32,
    name: String,
}

let examples = sqlx::query_as::<_, Example>(
    "SELECT id, name FROM example",
)
.fetch_all(&**pool)
.await?;

let json = serde_json::to_string(&examples);

Do you have any other good ideas?

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions