How to skip fields in serde serialization

Use the #[serde(skip_serializing)] attribute on a struct field to exclude it from serialization output.

Add the #[serde(skip_serializing)] attribute to the specific struct field you want to exclude from the output.

use serde::Serialize;

#[derive(Serialize)]
struct User {
    username: String,
    #[serde(skip_serializing)]
    password: String,
}

The password field will be present in the struct but omitted when serde_json::to_string(&user) is called.