Vec<T> from JSON Arrays

Convert JSON arrays into Rust Vec<T> with the element type inferred from the first item. Covers primitive arrays and arrays of objects.

Basics

Detailed Explanation

Mapping JSON Arrays to Vec

Every JSON array becomes a Vec<T> in Rust, where T is inferred from the first element. The converter handles primitive arrays and arrays of objects in the same pass.

Primitive arrays

{
  "tags": ["rust", "serde", "json"],
  "scores": [95, 87, 92]
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Root {
    pub tags: Vec<String>,
    pub scores: Vec<i32>,
}

Arrays of objects

{
  "users": [
    { "id": 1, "name": "Alice" },
    { "id": 2, "name": "Bob" }
  ]
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Root {
    pub users: Vec<User>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    pub id: i32,
    pub name: String,
}

The element struct name comes from the singular form of the array field name. users becomes User, categories becomes Category, and addresses becomes Addres (the singularizer is intentionally simple — adjust it manually for irregular plurals like children).

Empty arrays

An empty JSON array [] carries no element-type information. The converter falls back to Vec<serde_json::Value> so the code still compiles. Once you know the real shape, replace the inner type with the proper struct.

Heterogeneous arrays

Real-world arrays are usually homogeneous, but if your sample contains mixed shapes (one object with three fields, one with five), put the most complete element first or post-process the generated struct to wrap the missing fields in Option<T>.

Use Case

Paginated list endpoints, batch import payloads, and CSV-derived JSON dumps all show up as arrays of objects. Vec<T> with serde gives you safe iteration and field access in a single line of code.

Try It — JSON to Rust Struct Converter

Open full tool