Rust Application Config TOML to JSON

Convert Rust application configuration files from TOML to JSON. Covers the config crate pattern, environment-specific settings, and serde deserialization mapping.

Real-World Configs

Detailed Explanation

Rust applications commonly use TOML for configuration through the config crate or manual toml crate parsing. Understanding the JSON equivalent is valuable for interoperability, API responses, and multi-language environments.

A Rust application config.toml:

[app]
name = "web-service"
version = "2.1.0"
debug = false

[server]
host = "0.0.0.0"
port = 8080
workers = 4
max_connections = 1000
request_timeout = 30

[server.tls]
enabled = true
cert_path = "/etc/ssl/server.crt"
key_path = "/etc/ssl/server.key"

[database]
url = "postgres://user:pass@db:5432/myapp"
max_pool_size = 20
min_pool_size = 5
connect_timeout = 10
idle_timeout = 300

[database.migrations]
auto_run = true
directory = "./migrations"

[logging]
level = "info"
format = "json"
file = "/var/log/app.log"

[cache]
backend = "redis"
url = "redis://cache:6379"
ttl = 3600
prefix = "myapp:"

Converts to JSON:

{
  "app": {
    "name": "web-service",
    "version": "2.1.0",
    "debug": false
  },
  "server": {
    "host": "0.0.0.0",
    "port": 8080,
    "workers": 4,
    "max_connections": 1000,
    "request_timeout": 30,
    "tls": {
      "enabled": true,
      "cert_path": "/etc/ssl/server.crt",
      "key_path": "/etc/ssl/server.key"
    }
  },
  "database": {
    "url": "postgres://user:pass@db:5432/myapp",
    "max_pool_size": 20,
    "min_pool_size": 5,
    "connect_timeout": 10,
    "idle_timeout": 300,
    "migrations": {
      "auto_run": true,
      "directory": "./migrations"
    }
  },
  "logging": {
    "level": "info",
    "format": "json",
    "file": "/var/log/app.log"
  },
  "cache": {
    "backend": "redis",
    "url": "redis://cache:6379",
    "ttl": 3600,
    "prefix": "myapp:"
  }
}

Why this pattern is common in Rust:

  • The serde crate maps TOML tables directly to Rust structs. Each [section] becomes a struct field.
  • Integer types in TOML (like port = 8080) map precisely to Rust's i64 and then to JSON numbers.
  • The config crate supports layered configuration: TOML base file + environment variables + JSON overrides.

Practical conversion notes:

  • Snake_case keys (max_pool_size) are idiomatic in both Rust and TOML. They remain as-is in JSON.
  • Sub-tables like [server.tls] and [database.migrations] create nested objects that map to nested Rust structs.
  • No null values. Rust's Option<T> fields with None are simply omitted from the TOML file. In JSON, they could appear as null or be absent.

Use Case

Operating a Rust microservice that uses TOML for configuration but needs to expose its effective configuration as JSON through a /config health check endpoint, or sharing configuration between Rust and Node.js services.

Try It — TOML ↔ JSON Converter

Open full tool