Skip to content
🛠️ToolsShed

JSON to Rust Struct

Convert JSON objects to Rust struct definitions with serde derive macros.

About this tool

JSON to Rust Struct is a code generation tool that automatically converts JSON data into strongly-typed Rust struct definitions. Since Rust requires explicit type declarations, manually writing structs from JSON can be tedious and error-prone, especially for large or deeply nested objects. This tool eliminates that repetitive work by analyzing your JSON structure and generating struct code with serde derive macros for seamless serialization and deserialization.

Simply paste your JSON data into the input field, and the tool instantly generates Rust struct code ready to use in your project. The generated structs include standard serde annotations, making them compatible with popular Rust libraries like serde_json. This is particularly useful when working with external APIs, configuration files, or any JSON-based data that needs to be parsed into Rust.

The tool handles optional fields and nested objects intelligently, reducing manual adjustments needed before the code compiles. Whether you're building a web service, CLI tool, or integrating with REST APIs, this converter saves time and helps maintain type safety across your Rust codebase.

Frequently Asked Questions

Code Implementation

import json
from typing import Any

def to_pascal_case(s: str) -> str:
    return ''.join(word.capitalize() for word in s.replace('-', '_').split('_'))

def to_snake_case(s: str) -> str:
    import re
    s = re.sub(r'([A-Z]+)([A-Z][a-z])', r'\1_\2', s)
    s = re.sub(r'([a-z\d])([A-Z])', r'\1_\2', s)
    return s.lower().replace('-', '_')

def infer_type(value: Any, key: str, structs: dict) -> str:
    if value is None:
        return 'Option<serde_json::Value>'
    elif isinstance(value, bool):
        return 'bool'
    elif isinstance(value, int):
        return 'i64'
    elif isinstance(value, float):
        return 'f64'
    elif isinstance(value, str):
        return 'String'
    elif isinstance(value, list):
        if not value:
            return 'Vec<serde_json::Value>'
        return f'Vec<{infer_type(value[0], key, structs)}>'
    elif isinstance(value, dict):
        struct_name = to_pascal_case(key)
        collect_structs(value, struct_name, structs)
        return struct_name
    return 'serde_json::Value'

def collect_structs(obj: dict, name: str, structs: dict):
    fields = []
    for k, v in obj.items():
        snake = to_snake_case(k)
        rust_type = infer_type(v, k, structs)
        if snake != k:
            fields.append(f'    #[serde(rename = "{k}")]')
        fields.append(f'    pub {snake}: {rust_type},')
    structs[name] = fields

def json_to_rust(json_str: str, root_name: str = 'Root') -> str:
    data = json.loads(json_str)
    structs = {}
    collect_structs(data, root_name, structs)
    lines = []
    for name, fields in structs.items():
        lines.append('#[derive(Debug, Serialize, Deserialize)]')
        lines.append(f'pub struct {name} {{')
        lines.extend(fields)
        lines.append('}')
        lines.append('')
    return '\n'.join(lines)

json_input = '{"name": "Alice", "age": 30, "active": true}'
print(json_to_rust(json_input))

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.