Converting Between JSON and MAML on the Command Line
MAML is a minimal config format: JSON without mandatory commas and key quotes, plus comments and multiline strings. Fx reads MAML as input and writes it with the built-in MAML.stringify.
Convert MAML to JSON
Given config.maml:
maml
{
# Server settings
name: "my-app"
port: 8080
tags: ["web", "api"]
motd: """
Welcome!
Have a nice day.
"""
}Pass the . argument to print it as pretty JSON:
sh
fx config.maml .json
{
"name": "my-app",
"port": 8080,
"tags": [
"web",
"api"
],
"motd": "Welcome!\nHave a nice day.\n"
}Files ending in .maml are detected by extension. With stdin, use the --maml flag:
sh
cat config.maml | fx --maml .Save the result with fx config.maml . > config.json. Run fx config.maml without arguments to explore the file interactively.
Query MAML
Everything that works on JSON works on MAML:
sh
fx config.maml .port
fx config.maml '.tags.join(", ")'Convert JSON to MAML
Pipe JSON through MAML.stringify:
sh
fx package.json MAML.stringifymaml
{
name: "my-app"
version: "1.0.0"
scripts: {
test: "node test.js"
}
}Keys are quoted only when needed, and arrays and objects are written one item per line.
Parse MAML stored in a string
When a JSON field holds MAML text, decode it with MAML.parse:
sh
echo '{"config": "{a: 1, b: [2, 3]}"}' | fx .config MAML.parseNotes
- Key order is preserved.
- Comments are dropped, since JSON has no comments.
- Numbers keep their exact text, so
1.0stays1.0and long decimals keep all digits. - MAML integers are 64-bit; a larger integer is a syntax error.
- Duplicate keys are a syntax error.