jsondecode.com logo

JSON in Rust — serde_json Guide

The de-facto standard for JSON in Rust is serde_json, built on the serde serialization framework. Add serde = { version = "1", features = ["derive"] } and serde_json = "1" to your Cargo.toml.

Deserialize JSON into a struct

Derive Deserialize on your struct and use serde_json::from_str().

use serde::Deserialize;

#[derive(Deserialize, Debug)]
struct User {
    id: u32,
    name: String,
    email: String,
}

fn main() {
    let json = r#"{"id":1,"name":"Alice","email":"alice@example.com"}"#;
    let user: User = serde_json::from_str(json).unwrap();
    println!("{}", user.name); // Alice
}

Serialize a struct to JSON

Derive Serialize and use serde_json::to_string() or to_string_pretty().

use serde::Serialize;

#[derive(Serialize)]
struct User { id: u32, name: String }

let user = User { id: 1, name: "Alice".into() };

let json = serde_json::to_string(&user).unwrap();
// {"id":1,"name":"Alice"}

let pretty = serde_json::to_string_pretty(&user).unwrap();

Dynamic JSON with serde_json::Value

Use serde_json::Value for JSON with an unknown shape. Access fields with square-bracket indexing.

use serde_json::Value;

let json = r#"{"name":"Alice","scores":[95,87]}"#;
let v: Value = serde_json::from_str(json).unwrap();

println!("{}", v["name"]);          // "Alice"
println!("{}", v["scores"][0]);     // 95

Handle JSON errors in Rust

serde_json returns a Result type. Use the ? operator in functions that return Result, or match on the error.

use serde_json::Error;

fn parse_user(json: &str) -> Result<User, Error> {
    serde_json::from_str(json)
}

match parse_user(r#"{"id": "not_a_number"}"#) {
    Ok(user) => println!("Got: {}", user.name),
    Err(e) => eprintln!("Error at line {}, col {}: {}", e.line(), e.column(), e),
}

Frequently Asked Questions

How do I parse JSON in Rust?

Use serde_json::from_str(&json_string) with a struct that derives serde::Deserialize. Add serde and serde_json as dependencies in Cargo.toml.

What Cargo.toml dependencies do I need for JSON in Rust?

Add: serde = { version = "1", features = ["derive"] } and serde_json = "1". The derive feature enables the #[derive(Serialize, Deserialize)] macros.

How do I work with dynamic JSON in Rust?

Use serde_json::Value. Parse with: let v: Value = serde_json::from_str(json)?. Access fields with v["key"] (returns &Value) or v.get("key") (returns Option<&Value>).

How do I serialize a Rust struct to JSON?

Derive serde::Serialize on your struct and call serde_json::to_string(&value)? for compact output or serde_json::to_string_pretty(&value)? for indented output.

How do I rename JSON fields in Rust with serde?

Use the #[serde(rename = "json_key")] attribute on a field, or #[serde(rename_all = "camelCase")] on the struct to rename all fields according to a naming convention.

Format and validate your JSON instantly

Free, no ads, no sign-up. Also converts JSON to TypeScript, YAML, CSV, and more.

Open JSON Formatter →

If jsondecode.com saved you time, share it with your team

Free forever. No ads. No sign-up. Help other developers find it.