Handling Quoted Values in INI to JSON Conversion
Learn how single-quoted and double-quoted values in INI files are handled during conversion to JSON, including when quoting prevents type coercion.
Sections
Detailed Explanation
Quoted Values in INI Files
INI files support both single-quoted and double-quoted values. Quoting is important when a value contains characters that would otherwise be interpreted as part of the INI syntax, such as semicolons (inline comments), equals signs, or leading/trailing whitespace.
Example INI
[paths]
data_dir="/var/lib/myapp/data"
log_file='/var/log/myapp.log'
temp_dir = /tmp/myapp
[special]
; Value with semicolon needs quoting
connection_string="host=db;port=5432;user=admin"
greeting='Hello, World!'
numeric_string="12345"
bool_string="true"
[whitespace]
padded_value=" spaces preserved "
trimmed_value= spaces trimmed
Generated JSON
{
"paths": {
"data_dir": "/var/lib/myapp/data",
"log_file": "/var/log/myapp.log",
"temp_dir": "/tmp/myapp"
},
"special": {
"connection_string": "host=db;port=5432;user=admin",
"greeting": "Hello, World!",
"numeric_string": "12345",
"bool_string": "true"
},
"whitespace": {
"padded_value": " spaces preserved ",
"trimmed_value": "spaces trimmed"
}
}
Key Behaviors
- Quotes are stripped: The surrounding quotes are removed in the JSON output --- only the content between them is kept
- Type coercion is prevented:
"12345"remains the string"12345"instead of being coerced to the number12345. Similarly,"true"stays as the string"true" - Special characters preserved: Semicolons inside quotes are not treated as inline comments
- Whitespace handling: Inside quotes, whitespace is preserved exactly. Outside quotes, leading and trailing whitespace is trimmed
- Mixed quoting: Both single and double quotes work the same way. You cannot nest one type inside another
When to Use Quotes
- Values containing
;or#(to prevent inline comment parsing) - Values with leading or trailing whitespace you want to preserve
- Numeric or boolean strings that should remain as strings in JSON
- Values containing
=characters
Use Case
Parsing configuration files where values contain connection strings, file paths with special characters, or numeric identifiers that must remain strings (like ZIP codes or phone numbers) rather than being converted to numbers.