Convert Between YAML and JSON Config Formats
What It Is
A YAML/JSON converter translates structured data between YAML Ain't Markup Language (YAML) and JavaScript Object Notation (JSON). YAML relies on indentation for structure and supports comments and multi-line strings, making it popular for configuration files. JSON uses explicit brackets and is the universal data format for web APIs and JavaScript applications.
How It Works
YAML to JSON: the custom parser tokenizes the YAML input by indentation level, identifies key-value pairs and list items using an unquoted-colon detector, and builds a nested object tree. Values are parsed as primitives (numbers, booleans, null), quoted strings, inline arrays, or inline objects. The resulting object is serialized to formatted JSON. JSON to YAML: the JSON is parsed, then each value is recursively serialized to YAML with configurable indent size and quote style while respecting YAML quoting rules for strings containing colons or special characters.
Worked Example
Input YAML:server:\n host: localhost\n port: 8080
Output JSON:{
"server": {
"host": "localhost",
"port": 8080
}
}
Each YAML key-value pair becomes a JSON property. The indentation level in YAML determines nesting depth. The string "localhost" stays a string, while "8080" becomes a number because it is purely numeric. Comments in YAML are stripped and do not appear in the output.
Common Mistakes
- Inconsistent indentation in YAML. YAML requires every sibling at the same nesting level to have identical indentation. Mixing 2-space and 4-space indentation at the same level causes a parse error.
- Using tabs for YAML indentation. The YAML spec strongly discourages tabs. Always use spaces for indentation. This tool accepts tabs but normalizes them to spaces internally.
- Forgetting quotes around JSON keys containing special characters. JSON requires all string keys to be wrapped in double quotes. Keys like "server-port" are valid JSON but must be quoted, whereas in YAML quotes are optional for most plain keys.
Frequently Asked Questions
Q:What is the difference between YAML and JSON?
YAML uses indentation and is more human-readable with features like comments. JSON uses explicit braces and is stricter but natively supported in JavaScript. Both represent structured data but YAML is preferred for config files, JSON for APIs.
Q:Are my YAML/JSON files uploaded to servers?
No. All conversion happens client-side in your browser. Files are processed locally without being uploaded, making it safe for sensitive configuration files, API keys, or private data.
Q:What conversion errors can occur?
Common errors include invalid indentation in YAML, trailing spaces, and duplicate keys. JSON must have double quotes for keys and strings. The tool highlights syntax errors as you type and shows line numbers for quick fixes.
Q:Can YAML anchor and alias syntax be converted?
This converter uses a custom YAML parser that handles basic structures but does not support YAML anchors (&), aliases (*), or multi-document streams (---). For documents using these features, expand anchors to their full values before conversion.
Q:How does the tool handle comments during conversion?
YAML comments (lines starting with #) are stripped during conversion and do not appear in the JSON output. JSON does not support comments natively, so there is no way to preserve them in the target format. When converting JSON to YAML, inline comments can be added manually after conversion.