Parse Java files with the properties to JSON converter
This properties to JSON converter reads the Java .properties format as defined by java.util.Properties and writes it as JSON. It is useful for moving Spring Boot settings into a JSON-based config service, turning messages_en.properties bundles into i18n JSON for a JavaScript front end, or simply inspecting a large file as a tree.
Example
# messages
! legacy comment
app.name=Acme Shop
app.greeting=Hello, W\u00f6rld
app.welcome = Welcome \
back!
server.port: 8080
server.ssl.enabled true
path.home=C:\\apps\\acme
my\ key=spaced key
With both options on:
{
"app": {
"name": "Acme Shop",
"greeting": "Hello, Wörld",
"welcome": "Welcome back!"
},
"server": { "port": 8080, "ssl": { "enabled": true } },
"path": { "home": "C:\\apps\\acme" },
"my key": "spaced key"
}
Format rules supported
- Separators:
key=value,key: valueandkey value(whitespace) are all accepted, as in Java. - Comments: lines starting with
#or!. - Line continuations: a trailing backslash joins the next line, and its leading whitespace is dropped, so
Welcome \+back!givesWelcome back!. - Escapes:
\uXXXXUnicode escapes,\t,\n,\r,\f, an escaped backslash and escaped spaces or separators in keys such asmy\ key.
Options explained
Nest dotted keys splits keys on dots, so server.ssl.enabled becomes nested objects. Turn it off to keep one flat object with the original keys, which is what most i18n libraries expect for message bundles. Convert numbers & booleans turns 8080 and true into JSON types; with it off, every value is a string, exactly as Properties.getProperty returns it.
Moving message bundles to a JavaScript front end
Converting the file is the easy part; the message syntax inside the values also differs between Java and JavaScript i18n libraries:
- Placeholders. Java's
MessageFormatuses numbered arguments such asHello, {0}, while i18next expects named ones likeHello, {{name}}. Rename them after converting. - Doubled apostrophes. In messages that go through
MessageFormat, a single quote is an escape character, so bundles often containDon''t. A JavaScript library will show both quotes; replace''with'in those values. - Nesting. i18next splits keys on dots by default, so nested output works with
t('app.name')as is. If you keep flat keys, setkeySeparator: false.
Edge cases
- A key that is both a value and a parent. With
app=Acmefollowed byapp.name=x, JSON cannot hold both, so the later line wins andappbecomes{"name": "x"}. Use flat output to keep both. - Numeric segments such as
list.0=astay object keys ({"0": "a"}) when nesting, while Spring-style brackets likeactive[0]=devbecome real arrays. - Duplicate keys: the last one wins, matching Java.
Targeting Spring's YAML format instead? Use Properties to YAML. To go back, use JSON to Properties.
Privacy
Parsing runs locally in your browser, so datasource passwords in your properties file stay private.