Basic TOML to JSON Conversion
Learn the fundamentals of converting TOML to JSON format. Understand how TOML key-value pairs, strings, and numbers map to their JSON equivalents with clear examples.
Detailed Explanation
Converting TOML to JSON is a common task when integrating TOML-based configuration files with systems that expect JSON input. TOML (Tom's Obvious, Minimal Language) is designed to be easy to read due to its minimal syntax, and it maps cleanly to a JSON object (hash table).
A simple TOML document:
name = "devtoolbox"
version = "1.0.0"
private = true
port = 3000
Converts to this JSON:
{
"name": "devtoolbox",
"version": "1.0.0",
"private": true,
"port": 3000
}
Key differences in the conversion:
- Equals signs become colons. TOML uses
=to separate keys and values, while JSON uses:. - All keys get quoted in JSON. TOML keys are bare (unquoted) by default; JSON requires double quotes around every key.
- No commas between entries in TOML. Each key-value pair is on its own line. JSON requires commas between properties.
- Curly braces wrap the JSON object. TOML's top-level is implicitly a table (object); JSON requires explicit
{}.
Data types are preserved:
- TOML
"hello"(string) stays"hello"in JSON - TOML
42(integer) stays42in JSON - TOML
3.14(float) stays3.14in JSON - TOML
true(boolean) staystruein JSON
Unlike YAML, TOML has strict, unambiguous typing. There are no implicit type coercions -- true is always a boolean, "true" is always a string, and bare words without quotes are never valid string values. This makes TOML-to-JSON conversion highly predictable and safe.
Use Case
Converting a simple application configuration file from TOML to JSON when your deployment pipeline or runtime expects JSON configuration, such as migrating from a Rust project's config.toml to a JSON-based microservice configuration.