Convert INI Sections to Nested JSON Objects
Convert INI files with [section] headers into nested JSON objects. Each section becomes a top-level key containing its key-value pairs.
Basic Conversion
Detailed Explanation
Sections as Nested Objects
INI sections, delimited by [section_name], map naturally to nested JSON objects. The section name becomes a key in the root object, and all key-value pairs within that section become properties of the nested object.
Example INI
[database]
host=127.0.0.1
port=5432
name=myapp_production
[redis]
host=127.0.0.1
port=6379
db=0
[logging]
level=info
file=/var/log/myapp.log
Generated JSON
{
"database": {
"host": "127.0.0.1",
"port": 5432,
"name": "myapp_production"
},
"redis": {
"host": "127.0.0.1",
"port": 6379,
"db": 0
},
"logging": {
"level": "info",
"file": "/var/log/myapp.log"
}
}
How Section Mapping Works
- Each
[section]header creates a new nested object in JSON - Key-value pairs following the header are placed inside that object
- Keys before any section header go into the root object
- Section names can contain letters, numbers, hyphens, underscores, and dots
- Whitespace around section names is trimmed
Mixed Global and Section Keys
When an INI file has both global keys (before any section) and sections, the global keys remain at the root level alongside the section objects:
app_name=MyApp
[database]
host=localhost
Produces:
{
"app_name": "MyApp",
"database": {
"host": "localhost"
}
}
Use Case
Migrating a multi-section configuration file (e.g., a service configuration with separate database, cache, and logging sections) to a JSON-based config system used by a microservice framework.