---
title: How to deserialize JSON to structs
summary: Deserialize JSON to Rust structs using the serde crate with the Deserialize derive macro and serde_json parsing functions.
lang: en
slug: how-to-deserialize-json-to-structs
published: 2026-04-17T14:12:14+00:00
updated: 2026-04-25T22:00:13+00:00
canonical: https://www.rustfaq.org/en/how-to-deserialize-json-to-structs/
---

# How to deserialize JSON to structs



## The problem with JSON blobs

You are building a command-line tool. The user drops a `config.json` file into the project directory. You read the file and get a `String` containing text like `{"output_mode": "simple", "verbose": true}`. You have the data. You also have a `Config` struct in your code that defines the settings your application needs. The text and the struct are two different worlds. The text is a flat sequence of characters. The struct is a typed memory layout with fields. You cannot assign the string to the struct. You need to transform the JSON into the struct.

Writing a parser by hand is a trap. You will handle commas, quotes, and braces incorrectly. You will miss edge cases like escaped characters or nested objects. You will spend hours debugging whitespace issues. The Rust ecosystem has a standard solution. You use the `serde` crate. It handles the parsing, the type conversion, and the error reporting. You define the shape of your data. `serde` fills it in.

## Deserialization as assembly

Deserialization is the process of converting a serialized format like JSON into a native data structure. Think of it like a customs officer unpacking a suitcase. The suitcase is the JSON string. The items inside are the values. The officer holds a manifest, which is your Rust struct. The manifest lists exactly what should be in the suitcase and where each item belongs.

The officer opens the suitcase. They find a box labeled `output_mode`. They check the manifest. The manifest expects a string in the `output_mode` slot. The officer places the box there. They find a switch labeled `verbose`. The manifest expects a boolean. The officer flips the switch to match the JSON value. If the suitcase contains a item not on the manifest, the officer ignores it by default. If a required item is missing, the officer flags the shipment as invalid.

Serialization is the reverse process. You take the struct and pack it back into a JSON string. This article focuses on deserialization, the act of unpacking.

## The serde ecosystem

`serde` is not a single crate. It is a framework. The core `serde` crate defines the traits, like `Deserialize` and `Serialize`. These traits describe how a type can be converted to and from data formats. `serde` does not know about JSON. It knows about the abstract concept of data conversion.

Format-specific crates implement the traits for their format. `serde_json` implements `Deserialize` for JSON. `serde_yaml` does it for YAML. `bincode` does it for binary data. You add `serde` and `serde_json` to your `Cargo.toml`. The `serde` crate provides the derive macros. The `serde_json` crate provides the parsing functions.

Convention in the Rust community is to use `serde` for almost all data format conversions. It is fast, well-tested, and supported by the entire ecosystem. Do not write your own JSON parser. Reach for `serde`.

## Minimal example

Here is the smallest working example. You define a struct. You derive the `Deserialize` trait. You call `serde_json::from_str`.

```rust
use serde::Deserialize;

/// Configuration for the application output.
#[derive(Debug, Deserialize)]
struct Config {
    /// The mode for output formatting.
    output_mode: String,
    /// Whether to enable verbose logging.
    verbose: bool,
}

fn main() {
    // The JSON string matches the struct fields exactly.
    let json = r#"{"output_mode": "simple", "verbose": true}"#;
    
    // from_str returns a Result. unwrap panics if parsing fails.
    let config: Config = serde_json::from_str(json).unwrap();
    
    println!("{:?}", config);
}
```

The `#[derive(Deserialize)]` attribute is a procedural macro. It runs at compile time. It generates the implementation of the `Deserialize` trait for your `Config` struct. The generated code matches JSON keys to struct fields by name. It converts JSON strings to Rust `String` types. It converts JSON booleans to Rust `bool` types. You do not write this code. The macro writes it for you.

The `serde_json::from_str` function takes a `&str`. It parses the string. It returns a `Result<Config, serde_json::Error>`. The `Result` type forces you to handle errors. If the JSON is invalid, or if a field is missing, the function returns an error. You use `.unwrap()` here for brevity. In production code, you handle the error properly.

Trust the derive macro. It generates the boilerplate so you don't have to.

## How the compiler helps you

The compiler catches mistakes before you run the code. If you forget to derive `Deserialize`, the compiler rejects your code.

```rust
use serde::Deserialize;

// Missing #[derive(Deserialize)]
struct Config {
    output_mode: String,
}

fn main() {
    let json = r#"{"output_mode": "simple"}"#;
    // This line fails to compile.
    let config: Config = serde_json::from_str(json).unwrap();
}
```

The compiler produces error `E0277` (trait bound not satisfied). The message says `the trait Deserialize is not implemented for Config`. The error points to the `from_str` call. It tells you exactly what is missing. You add the derive attribute. The error disappears.

If you use a type that `serde` does not know how to deserialize, you get the same error. `serde` supports standard library types like `String`, `bool`, `i32`, `Vec<T>`, and `Option<T>`. It supports enums with specific representations. It does not support custom types unless you derive `Deserialize` for them or implement the trait manually.

Treat the compiler error as a guide. It tells you exactly which field failed.

## Handling real-world JSON

Real-world JSON rarely matches Rust naming conventions perfectly. JSON APIs often use kebab-case (`output-mode`) or camelCase (`outputMode`). Rust uses snake_case (`output_mode`). You can bridge the gap with serde attributes.

### Renaming fields

You can rename individual fields using `#[serde(rename = "json_key")]`. This tells serde to look for `json_key` in the JSON and map it to the Rust field.

```rust
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Config {
    // Map "output-mode" from JSON to "output_mode" in Rust.
    #[serde(rename = "output-mode")]
    output_mode: String,
}
```

### Renaming all fields

If your JSON uses a consistent naming style, you can rename all fields at once using `#[serde(rename_all = "style")]`. Supported styles include `kebab-case`, `camelCase`, `PascalCase`, `snake_case`, and `SCREAMING_SNAKE_CASE`.

```rust
use serde::Deserialize;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
struct Config {
    // Matches "output-mode" in JSON.
    output_mode: String,
    // Matches "verbose" in JSON.
    verbose: bool,
}
```

This attribute applies to every field in the struct. It saves you from adding `rename` to every field. It also keeps your Rust code idiomatic. You keep snake_case in Rust. You match the JSON style at the boundary.

Convention aside: Use `rename_all` at the struct level whenever the JSON style differs from Rust. It reduces boilerplate and prevents typos in individual renames.

### Nested structures

JSON often contains nested objects. Rust handles this with nested structs. You define a struct for the nested object. You derive `Deserialize` for it. You use it as a field type in the parent struct.

```rust
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct AppSettings {
    // The database field maps to the "database" object in JSON.
    database: DatabaseConfig,
    // The features field maps to the "features" array in JSON.
    features: Vec<String>,
}

#[derive(Debug, Deserialize)]
struct DatabaseConfig {
    host: String,
    port: u16,
}
```

The JSON looks like this:

```json
{
  "database": {
    "host": "localhost",
    "port": 5432
  },
  "features": ["auth", "logging"]
}
```

Serde recursively deserializes the nested structures. It parses the `database` object into a `DatabaseConfig`. It parses the `features` array into a `Vec<String>`. The process is automatic. You just define the shapes.

Match the JSON keys to your fields explicitly. Ambiguity causes runtime errors.

## Error handling that doesn't panic

The `from_str` function returns a `Result`. You must handle the error. Using `.unwrap()` is fine for examples. It panics if the JSON is invalid. In production code, you want to report the error to the user or log it.

```rust
use serde::Deserialize;
use std::fs;

#[derive(Debug, Deserialize)]
struct Config {
    output_mode: String,
}

fn load_config(path: &str) -> Result<Config, Box<dyn std::error::Error>> {
    // Read the file contents.
    let contents = fs::read_to_string(path)?;
    
    // Parse the JSON.
    let config: Config = serde_json::from_str(&contents)?;
    
    Ok(config)
}
```

The `?` operator propagates errors. If `read_to_string` fails, it returns the error. If `from_str` fails, it returns the error. The function returns a `Result`. The caller decides what to do.

Serde errors are descriptive. They tell you the line and column where the error occurred. They tell you what was expected and what was found. You can print the error to help the user fix their JSON.

```rust
match load_config("config.json") {
    Ok(config) => println!("Loaded config: {:?}", config),
    Err(e) => eprintln!("Failed to load config: {}", e),
}
```

The error message might say `missing field `output_mode` at line 1 column 2`. This is actionable. The user knows exactly what to fix.

Don't swallow errors. Surface them with context.

## Dynamic JSON vs Static structs

Sometimes you do not know the structure of the JSON ahead of time. Maybe you are building a proxy that forwards JSON. Maybe you are processing data from an API that changes frequently. In these cases, static structs are too rigid. You can deserialize into `serde_json::Value`.

`Value` is an enum that represents any valid JSON value. It can be a null, a boolean, a number, a string, an array, or an object. It holds the data dynamically.

```rust
use serde_json::Value;

fn main() {
    let json = r#"{"name": "Alice", "age": 30}"#;
    let value: Value = serde_json::from_str(json).unwrap();
    
    // Access fields dynamically.
    if let Some(name) = value.get("name").and_then(|v| v.as_str()) {
        println!("Name: {}", name);
    }
}
```

Using `Value` gives you flexibility. You can inspect the JSON at runtime. You can extract parts of it. You can modify it. The trade-off is performance and safety. You lose compile-time checks. You have to handle `Option` and type conversions manually. You pay a small performance cost for the dynamic dispatch.

Use `Value` when the schema is unknown or highly variable. Use structs when the schema is known. Structs are faster and safer.

Pick the tool that matches your certainty about the schema.

## Advanced attributes

Serde provides attributes for common edge cases.

### Default values

If a field is missing in the JSON, deserialization fails by default. You can provide a default value using `#[serde(default)]`. This tells serde to use the `Default` trait implementation for the field type if the key is missing.

```rust
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Config {
    output_mode: String,
    // If "verbose" is missing, use false.
    #[serde(default)]
    verbose: bool,
}
```

You can also provide a custom default function using `#[serde(default = "module::function")]`. This gives you full control over the fallback value.

### Optional fields

If a field might be present or absent, use `Option<T>`. Serde automatically handles `Option`. If the key is present, it deserializes into `Some(value)`. If the key is missing, it deserializes into `None`.

```rust
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Config {
    output_mode: String,
    // "debug_port" is optional.
    debug_port: Option<u16>,
}
```

This is cleaner than using `default` for optional data. It makes the optionality explicit in the type system. You check for `None` at runtime.

### Aliases

If a JSON field might appear under multiple names, you can use `#[serde(alias = "name")]`. This allows serde to accept any of the aliases.

```rust
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Config {
    // Accepts "output_mode" or "outputMode".
    #[serde(alias = "outputMode")]
    output_mode: String,
}
```

This is useful for backward compatibility. If you rename a field in your API, you can keep the old name as an alias during the transition period.

Use `Option<T>` for optional data. Use `default` for missing data with a fallback. Use `alias` for backward compatibility.

## Pitfalls and compiler errors

### Missing fields

If a required field is missing in the JSON, serde returns an error. The error message identifies the missing field.

```text
Error: missing field `output_mode` at line 1 column 2
```

This is a runtime error. The compiler cannot catch it because the JSON is external data. You must handle the error in your code.

### Type mismatches

If a JSON value does not match the expected type, serde returns an error. For example, if the JSON has a string where a boolean is expected.

```text
Error: invalid type: string "true", expected boolean at line 1 column 15
```

This error is also runtime. It tells you the type mismatch and the location. You can fix the JSON or adjust your struct.

### Unknown fields

By default, serde ignores unknown fields in the JSON. This is safe. It allows the JSON to evolve without breaking your code. If you want to be strict, you can use `#[serde(deny_unknown_fields)]`. This makes serde reject any JSON with extra fields.

```rust
use serde::Deserialize;

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct Config {
    output_mode: String,
}
```

If the JSON contains an extra field, deserialization fails. This is useful for configuration files where you want to catch typos. It is less useful for API responses where the server might add new fields.

Choose strictness based on your trust in the data source.

## Decision matrix

Use `serde_json::from_str` when you have a JSON string and want to deserialize it into a struct. It validates UTF-8 and parses the string. Use `serde_json::from_slice` when you have JSON bytes, such as from a file or network request. It skips UTF-8 validation, which can be slightly faster for binary data. Use `serde_json::from_value` when you already have a `serde_json::Value` and want to convert it to a struct. This is useful for extracting parts of a larger JSON document. Use `#[serde(rename_all = "kebab-case")]` when your JSON uses a naming convention that differs from Rust's snake_case. It applies the rename to all fields automatically. Use `Option<T>` when a field might be missing in the JSON. It represents the absence of data explicitly. Use `#[serde(default)]` when a field is missing but you want a fallback value. It uses the `Default` trait to fill in the gap. Use `serde_json::Value` when the JSON structure is unknown or highly dynamic. It allows runtime inspection of the data. Use `#[serde(deny_unknown_fields)]` when you want to reject JSON with extra fields. It enforces a strict schema.

Pick the tool that matches your data source and your certainty about the schema.

## Where to go next

- [How to use serde with CSV](/en/how-to-use-serde-with-csv/)
- [How to handle optional fields with serde](/en/how-to-handle-optional-fields-with-serde/)
- [How to handle dates with serde](/en/how-to-handle-dates-with-serde/)