Dev · 108 tools · nothing uploads

JSON to Rust

JSON to Rust. Infers types from an example JSON document. Nested objects become named types by their key; mixed-type arrays fall back to a broad/any type. Runs on your device.

Input

live

JSON to Rust writes a struct for each object in the example, every one carrying #[derive(Debug, Serialize, Deserialize)]. A key that is not lowercase snake_case is renamed with a serde attribute back to its original spelling. Nothing else is emitted: no use statements, so serde and serde_json belong in your Cargo.toml already.

Questions

What does the generated code look like?

One struct per object in your example, each with #[derive(Debug, Serialize, Deserialize)] above it. The top-level struct is called Root, nested ones are named after the key that holds them, and they are printed innermost first so the file compiles top to bottom with no forward references.

Which Rust types does it use?

String for strings, bool for booleans, i64 for whole numbers and f64 for fractional ones. An array becomes Vec<T> with T taken from its first element, and a nested object becomes a struct named after its key. A null becomes Option<serde_json::Value>, and an empty array's element type becomes serde_json::Value.

Do I need serde in my project?

Yes. The derive names Serialize and Deserialize, and some fields can mention serde_json::Value, so add serde with the derive feature and serde_json to your Cargo.toml. The tool emits no use statements or attributes beyond the derive, so bring those into scope yourself.

What happens to keys that are not snake_case?

They get a rename attribute. A key that does not match lowercase letters, digits and underscores gets #[serde(rename = "...")] with the original key, and the field itself is lowercased with the other characters replaced by underscores. So userName becomes a username field with a rename back to userName.

Are fields optional?

No. Every field is required, because the generator sees a single example and cannot tell which keys might be missing from another response. Wrap the ones that can be absent in Option yourself, or add serde defaults. The same applies to a field that holds more than one type, which needs an enum you write by hand.

Does my JSON get uploaded?

No. Parsing and code generation run in a Web Worker in this tab. That matters when the example you drop in is a real API response with live values in it, since none of it leaves your machine. Nothing is uploaded, nothing is logged, and the page works offline after the first load.

Related Dev tools

n→nJSON format / validateParse, validate and pretty-print JSON, with a clear parse-error message if it is broken.Runs on each file you dropn→nJSON sort / minify / escapeSort object keys, minify to one line, or escape/unescape JSON for embedding inside another string.Runs on each file you dropn→nJSONPath queryQuery JSON with a JSONPath expression: dot/bracket access, wildcards (*), recursive descent (..). No filter expressions (?()) or slices; those need a full JSONPath engine this build does not have.Runs on each file you dropn→nJSON Schema generatorInfer a draft-07 JSON Schema from an example JSON document. Every observed key is marked required, arrays are typed to accept every observed element.Runs on each file you dropn→nJSON to TypeScriptJSON to TypeScript. Infers types from an example JSON document. Nested objects become named types by their key; mixed-type arrays fall back to a broad/any type.Runs on each file you dropn→nJSON to GoJSON to Go. Infers types from an example JSON document. Nested objects become named types by their key; mixed-type arrays fall back to a broad/any type.Runs on each file you dropn→nJSON ↔ CSVConvert a flat JSON array of objects to CSV and back. CSV has no type system: JSON->CSV renders numbers/booleans/null as text and CSV->JSON always comes back as strings. A nested object or array field is refused by name rather than silently flattened. CSV parsing is fully quote-aware, embedded newlines included; a file with an unterminated quote is refused rather than guessed at.Runs on each file you dropn→n.env ↔ JSONConvert a .env file to a flat JSON object and back. Values are treated as strings both ways. No nesting, no type coercion of numbers/booleans on the JSON->env direction beyond JSON.stringify.Runs on each file you drop