Convert Java properties to YAML
What changes when Java .properties files become YAML: nesting, type inference, quoting, empty values and comments, and when to keep properties instead.
Converting a Java .properties file to YAML changes the file's structure. Flat dotted keys become nested mappings, indexed keys can become lists, and every value goes through YAML's own rules for types, quoting and comments. This review covers what changes, where meaning can shift without any warning, and when keeping the original format is the better choice. It is based on the documented behavior of both formats. No specific converter was tested, and none is rated here.
Short verdict
Conversion is worth doing when two things are true. First, the program that reads the file accepts both formats, as Spring Boot does for application configuration. Second, the file has many keys that share prefixes. The main benefit is readability: each shared prefix is written once, and lists become real sequences. The decisive tradeoff is that YAML depends on whitespace and infers types. A value that was an ordinary string in the properties file can become a boolean, a number, null or a shortened string unless it is quoted. Plain properties are the better choice in these cases:
- The consumer reads only properties. Examples are code that calls java.util.Properties directly, ResourceBundle message bundles, and Spring's @PropertySource annotation, which the Spring Boot documentation says does not load YAML files by default.
- The file is short and flat, so nesting adds indentation without removing much repetition.
- People regularly search for full keys such as app.datasource.username, which no longer appear as one string once nested.
- The configuration is passed around through chat, email, tickets or web forms that may change leading whitespace.
- Many values contain characters that YAML treats specially, so most lines would need quoting anyway.
What the conversion actually changes
This conversion does not reverse, mirror or restyle any characters. It rewrites the file's structure and syntax into a different grammar. In this context, "reverse" usually means the opposite conversion, YAML back to properties, which flattens nested keys into dotted ones.
In a properties file, a dot inside a key is just a character. The key app.datasource.username is a single opaque key, and every value is a string. A YAML converter treats each dot as a level of nesting by convention. The YAML parser that later reads the file then decides each value's type from how it is written. The specific changes are listed below.
- Hierarchy: dotted keys are split into nested mappings, indented with spaces. YAML does not allow tabs for indentation.
- Lists: index notation such as servers[0] and servers[1] becomes a YAML sequence only if the converter understands that convention, which Spring Boot uses. Otherwise the brackets stay inside the key name.
- Types: unquoted values such as yes, no, on, off, true, 10 or 0042 may be read as booleans or numbers. Under YAML 1.1 rules, which many Java YAML parsers follow, yes, no, on and off are booleans, and a number with a leading zero can be read as octal. Under YAML 1.2 core rules, 0042 is the integer 42. In both cases the original string is lost.
- Empty values: key= in properties is an empty string. A YAML key with nothing after the colon is null.
- Comments and special characters: in properties, # starts a comment only at the beginning of a line, so it is literal inside a value. In YAML, a # preceded by a space starts a comment. Values also need quotes if they begin with characters such as *, &, !, %, @, {, [ or a backtick, or if they contain a colon followed by a space.
- Escapes and encoding: \uXXXX escapes are decoded into real characters. These escapes are common because Properties.load(InputStream) reads ISO-8859-1. YAML files are normally UTF-8, so the output should be saved as UTF-8.
- Line continuations: a trailing backslash joins lines in properties and discards leading whitespace on the next line. The converter should produce either a single value or a YAML block scalar with the same content.
- Separators and whitespace: properties accept =, : or whitespace between key and value, and they trim whitespace before the value but keep trailing spaces. YAML drops trailing spaces from unquoted values, so any value that depends on them must be quoted.
- Comments and order: some converters drop comments or sort keys. This does not change what the application reads, but it does change what reviewers see.
Worked example: where meaning shifts
The input below is a small properties file. Its values are illustrative only.
# Inventory service settings
app.name=Inventory Service
app.datasource.username=svc_inventory
app.datasource.pool-size=10
app.feature.enabled=yes
app.region.code=0042
app.servers[0]=alpha
app.servers[1]=beta
app.greeting=Caf\u00e9 #1
app.optional=
Suppose a converter only splits keys on dots, writes values without quotes and drops comments. It would produce something like this:
app:
name: Inventory Service
datasource:
username: svc_inventory
pool-size: 10
feature:
enabled: yes
region:
code: 0042
servers:
- alpha
- beta
greeting: Café #1
optional:
The output looks correct, but a YAML parser reads several values differently from the original:
- enabled: yes becomes the boolean true under YAML 1.1 rules, instead of the string "yes".
- code: 0042 becomes a number (34 under YAML 1.1 octal rules, 42 under YAML 1.2), instead of the string "0042".
- greeting: Café #1 is read as just "Café", because the space followed by #1 is treated as a comment.
- optional: becomes null instead of an empty string.
- pool-size: 10 becomes the integer 10. Frameworks that bind values to typed fields generally accept this, but a consumer that expects every value to be a string may not.
- The opening comment is gone.
A conversion that keeps the original meaning quotes the affected values and keeps the comment:
# Inventory service settings
app:
name: Inventory Service
datasource:
username: svc_inventory
pool-size: 10
feature:
enabled: "yes"
region:
code: "0042"
servers:
- alpha
- beta
greeting: "Café #1"
optional: ""
The escape \u00e9 in the original decodes to the single character é. The YAML file must be saved as UTF-8 for that character to survive. If the file is later saved in a different encoding, the character can be replaced or corrupted.
Conflicts that cannot be converted directly
Some valid properties files have no direct nested YAML equivalent. If a file contains both app.cache=redis and app.cache.ttl=60, then app.cache would have to be a string and a mapping at the same time. A careful converter reports this instead of quietly keeping one of the two. The fix is to rename a key or keep that part of the configuration in properties form. Duplicate keys are another problem. Properties files keep the last value without any warning, while the YAML specification requires mapping keys to be unique, and many parsers reject duplicates. The converted file should contain only the value that was actually in effect. Keys with escaped characters, such as an escaped space or colon, also need checking, because they must become quoted YAML keys.
Will the result stay readable and usable after copying or sharing?
Properties files are line-oriented: each line stands on its own, so a pasted fragment still carries its full key. YAML depends on indentation, which makes it more fragile when it is copied or sent somewhere.
- Copy and paste: chat tools, email clients, word processors and some web forms can strip leading spaces, turn spaces into tabs or insert non-breaking spaces. Any of these can break parsing. Worse, they can move a key to a different level without causing any error. Pasting into a code block or attaching the file avoids most of this. Check the pasted copy by parsing it, not by looking at it.
- Partial snippets: a few lines copied from the middle of a YAML file lose their parent keys. Sharing app.datasource.username=svc_inventory is unambiguous; sharing username: svc_inventory on its own is not.
- Search and editing: searching for a full dotted key finds it in a properties file but not in nested YAML, where the parts sit on separate lines. Editors with YAML support can show the full path, but plain search tools cannot. Hand edits to YAML can also introduce indentation mistakes that a properties file cannot have.
- Accessibility: in nested YAML, a line's meaning depends on its indentation. A screen reader user has to track that, either by having indentation announced or by moving to the parent lines. A flat properties line states its whole key. This is a reasoned expectation rather than a tested result. No screen reader was tested for this review, and behavior will vary with the reader and the editor settings.
- Tool compatibility: every tool that reads the file must accept YAML. Spring Boot does for application configuration, but a library or script that reads the file with java.util.Properties will not.
Keep a plain properties version, either the original file or a flattened export, in any of these situations: the configuration is shared outside source control, it is pasted into tickets, reviewers search by full key, or a tool reads it that only understands properties.
Converting back to properties
Converting from YAML to properties flattens nested keys into dotted ones and turns sequences into indexed keys. For plain string values, this round-trips well. Several YAML-only features do not survive the trip. Anchors and aliases are expanded into repeated values. Multi-document files have to be split or handled with framework-specific conventions. Type information is lost because every properties value is a string. Comments may also be dropped.
How to check a conversion
- 1. Load the original properties file and record every key and value as strings.
- 2. Parse the YAML output with the parser, and parser version, that the application uses, since YAML 1.1 and 1.2 rules differ.
- 3. Flatten the parsed YAML back into dotted keys and turn each value into its string form.
- 4. Compare the two sets of keys and values. Look especially for booleans, numbers, nulls, shortened strings and missing keys.
- 5. Run the application or its configuration tests against the YAML file before removing the properties file.
Privacy when using online converters
Configuration files often contain credentials, internal host names and connection strings. A converter's interface or marketing cannot tell you whether it stores or logs what you paste. Only its published policy, or running the conversion locally, answers that question. If in doubt, remove secrets before converting, or use an offline tool or a short script.
What this review could not verify
No converter was tested, so this review does not rate how any particular tool handles comments, key order, index notation or conflicts. The points about type inference depend on which YAML version a parser implements, and the accessibility point is reasoned rather than measured. Running a specific tool on the example above is the most direct way to close these gaps.