JSON to Rust struct
Paste JSON and get Rust structs for it below, as you type, deriving serde's Serialize and Deserialize, ready for serde_json::from_str.
Fields in snake_case
Fields are in snake_case, with #[serde(rename = "…")] where the key is written otherwise (firstName, 2fa). A key that is a Rust keyword becomes a raw identifier (r#type), which serde reads by its plain name.
The types
Whole numbers are i64 and decimals f64; lists are Vec<T>, objects keyed by ids or dates HashMap<String, T>, and values of mixed kinds serde_json::Value. A value that may be null or missing is an Option, which serde reads as None when the key isn't there.
Types from sample data
Every item of a list, and every value of JSON Lines, is a sample of the same type: a key that some lack is optional, a value that is sometimes null is nullable, whole numbers mixed with decimals are decimals, and objects with the same keys share one type, named after the key they are under ("line_items" holds LineItem). An object whose keys are ids or dates, such as {"1001": {…}, "1002": {…}}, is a map. YAML, TOML, CSV and the other formats work too: pick their language above the editor.
Or the data with json!
Data writes the JSON in serde_json's json! macro, which takes JSON as it is; integers too big for an i32 literal get a suffix (3000000000i64), as Rust needs.
Copy, download or switch
The Rust follows your typing, and a mistake in the JSON is pointed out first, with its line and column. Copy or Download (a .rs file) take it as it is. Above the code, switch between Types and Data, or pick another language; the Convert menu's Code item opens the one you picked last.
Private: nothing is uploaded
Formatting, checking and comparing all happen in your browser. Your documents are saved in the browser's own storage on this computer, not on a server, so they never leave your device.
With your permission, we count anonymous visits (which pages and tools are used), never what you type. See Privacy.
Questions
Which crates do I need?
serde with its derive feature, and serde_json: cargo add serde --features derive, then cargo add serde_json.
Is my data uploaded anywhere?
Your documents aren't: everything runs in your browser, and they are saved on your computer, in the browser's storage for this site. With your permission we count anonymous visits, never their content.
More tools
- Formatter & Converter for JSON, YAML, XML, SQL
- JSON to TypeScript – Interfaces from JSON
- JSON to Go Struct Converter
- JSON to C# Class Converter
- JSON to Java Class & Record Converter
- JSON to Python Dataclass, Pydantic & Dict
- JSON to Kotlin Data Class Converter
- JSON to Dart Model with fromJson & toJson
- JSON to Swift Codable Struct Converter
- Free Online Diagram Maker & Editor
- Crypto Tools: Passwords, Keys, Hashes & JWTs
- Timestamp, Cron, Regex & UUID Utilities