Handling Duplicate Keys in INI to JSON Conversion
Learn the two strategies for handling duplicate keys in INI files: 'last wins' (standard behavior) and 'as array' for collecting multiple values.
Detailed Explanation
Duplicate Keys in INI Files
Some INI-style configuration files use duplicate keys within the same section to represent lists. For example, the PHP error_handler or Samba's interfaces directive may appear multiple times. The converter provides two strategies for handling this.
Example INI
[dns]
nameserver=8.8.8.8
nameserver=8.8.4.4
nameserver=1.1.1.1
[allowed_hosts]
host=web1.example.com
host=web2.example.com
host=web3.example.com
host=web4.example.com
[app]
log_level=debug
log_level=info
Strategy 1: Last Wins (Default)
Standard INI behavior --- when a key appears multiple times, only the last value is kept.
{
"dns": {
"nameserver": "1.1.1.1"
},
"allowed_hosts": {
"host": "web4.example.com"
},
"app": {
"log_level": "info"
}
}
Strategy 2: As Array
All values for duplicate keys are collected into a JSON array, preserving the order.
{
"dns": {
"nameserver": ["8.8.8.8", "8.8.4.4", "1.1.1.1"]
},
"allowed_hosts": {
"host": [
"web1.example.com",
"web2.example.com",
"web3.example.com",
"web4.example.com"
]
},
"app": {
"log_level": ["debug", "info"]
}
}
Choosing a Strategy
| Use "Last wins" when... | Use "As array" when... |
|---|---|
| You want standard INI semantics | Keys represent lists of values |
| Override patterns are intentional | All values should be preserved |
| Simpler JSON output is preferred | The consuming app expects arrays |
Round-Trip Behavior
When converting "As array" JSON back to INI, each array element is emitted as a separate line with the same key, faithfully reproducing the original multi-value structure.
Use Case
Parsing DNS resolver configuration, Samba server settings, or any configuration where repeated keys represent a list of values that should be preserved as an array in the JSON output.