Nested TOML Tables and Deep JSON Objects

Understand how deeply nested TOML tables using dotted keys and hierarchical headers translate to nested JSON objects. Learn best practices for managing complexity.

TOML to JSON

Detailed Explanation

TOML supports nested tables through dotted keys and hierarchical table headers, allowing you to represent deeply nested JSON structures. However, TOML's flat appearance can make deep nesting more verbose than JSON's brace-based approach.

Nested tables with dotted headers:

[server.http]
host = "0.0.0.0"
port = 3000

[server.https]
host = "0.0.0.0"
port = 443
cert = "/etc/ssl/cert.pem"

[logging.output]
console = true
file = "/var/log/app.log"

Converts to JSON:

{
  "server": {
    "http": {
      "host": "0.0.0.0",
      "port": 3000
    },
    "https": {
      "host": "0.0.0.0",
      "port": 443,
      "cert": "/etc/ssl/cert.pem"
    }
  },
  "logging": {
    "output": {
      "console": true,
      "file": "/var/log/app.log"
    }
  }
}

Dotted keys for shallow nesting:

[server]
http.host = "0.0.0.0"
http.port = 3000
https.host = "0.0.0.0"
https.port = 443

This produces the same JSON structure but with a different visual layout. Dotted keys keep related values together under one table header.

Super tables (implicitly created):

[a.b.c]
key = "value"

This implicitly creates tables a and a.b even though they were never explicitly declared. The JSON output is:

{
  "a": {
    "b": {
      "c": {
        "key": "value"
      }
    }
  }
}

Practical considerations:

  • TOML works best with 2-3 levels of nesting. Beyond that, table headers become long and unwieldy ([a.b.c.d.e]).
  • Unlike JSON, you cannot visually see the nesting depth from indentation -- the header path is the only indicator.
  • When converting deeply nested JSON to TOML, consider if the structure can be flattened or if JSON/YAML might be a better fit for that particular configuration.

Use Case

Organizing a multi-component application configuration with nested sections for server, database, cache, and logging subsystems, where each component has its own sub-configuration in TOML format.

Try It — TOML ↔ JSON Converter

Open full tool