Handling Inline Comments in INI Files
Understand how inline comments (text after ; or # on a key-value line) are handled during INI to JSON conversion, including edge cases with quoted values.
Detailed Explanation
Inline Comments in INI
Unlike full-line comments, inline comments appear at the end of a key-value line, separated by whitespace followed by ; or #. The converter strips these from the value during parsing.
Example INI
[server]
host=0.0.0.0 ; listen on all interfaces
port=8443 # HTTPS port
workers=4 ; one per CPU core
timeout=30 ; seconds
bind_address=127.0.0.1 ; loopback only in dev
[paths]
; Full-line comment preserved as-is
data="/var/lib/app;data" ; semicolon inside quotes is NOT a comment
cache_dir=/tmp/cache # cleared on reboot
Generated JSON
{
"server": {
"host": "0.0.0.0",
"port": 8443,
"workers": 4,
"timeout": 30,
"bind_address": "127.0.0.1"
},
"paths": {
"data": "/var/lib/app;data",
"cache_dir": "/tmp/cache"
}
}
Parsing Rules for Inline Comments
- The parser looks for whitespace followed by
;or#in the unquoted portion of the value - Everything from the comment marker to the end of the line is stripped
- Semicolons or hashes inside quoted values are not treated as comments
- The value is then trimmed of any trailing whitespace left by the removal
Edge Cases
| INI line | Parsed value | Notes |
|---|---|---|
key=value ; comment |
"value" |
Standard inline comment |
key="val;ue" ; comment |
"val;ue" |
Quoted semicolons preserved |
key=value;no_space |
"value;no_space" |
No space before ; — not a comment |
key= ; comment |
"" |
Empty value with comment |
Difference from Full-Line Comments
Full-line comments (lines starting with ; or #) can optionally be preserved in JSON. Inline comments are always stripped during conversion because there is no standard way to associate them with a specific key in JSON.
Use Case
Parsing server or application configuration files where administrators have added inline documentation to individual settings, and you need to extract only the actual values for use in a deployment automation pipeline.