YAML to JSON Converter
Instantly convert YAML to JSON or JSON to YAML. Handles multi-line strings, anchors, and nested structures.
js-yaml parser, which follows the YAML 1.2 specification. If your input contains syntax errors (like bad indentation), the tool will display a clear error message pointing to the exact line.
What is YAML and JSON?
YAML (YAML Ain't Markup Language) and JSON (JavaScript Object Notation) are two of the most widely used data serialization formats in modern software development. JSON is the native language of web APIs and JavaScript applications. YAML is a human-readable superset of JSON, designed specifically for configuration files. It is used extensively by Docker Compose, Kubernetes, Ansible, GitHub Actions, and CI/CD pipelines.
While JSON uses brackets and braces to define structure, YAML uses indentation and line breaks. This makes YAML significantly easier for humans to read and write, but it also makes it more error-prone. A single space or tab in the wrong place can completely change the meaning of a YAML document, or cause it to fail parsing entirely. This is why developers constantly need a reliable YAML to JSON converter—to validate their YAML and see the explicit structure underneath.
Why Convert YAML to JSON?
You might wonder why you would ever convert a human-readable format like YAML into the more verbose JSON. The answer is interoperability. Here are the most common scenarios:
- API Consumption: APIs accept and return JSON, not YAML. If you have a YAML configuration file that you need to send to an API, you must convert it to JSON first.
- Schema Validation: JSON Schema is a powerful tool for validating the structure of JSON documents. YAML does not have a native schema standard with the same level of tooling support. Developers often convert YAML to JSON specifically so they can validate it against a JSON Schema[reference:3].
- Debugging: YAML's implicit typing can be confusing. For example,
version: 1.0might be parsed as a float, whileversion: "1.0"is a string. Converting to JSON makes the data types explicit, helping you catch bugs. - Legacy Systems: Some older systems and databases only accept JSON. If your configuration is in YAML, you need a converter to ingest it.
The Core Differences: Syntax and Data Types
YAML and JSON represent the same underlying data structures (objects, arrays, strings, numbers, booleans, and null), but their syntax differs significantly:
- Objects: JSON uses curly braces
{}. YAML uses indentation with key-value pairs:key: value. - Arrays: JSON uses square brackets
[]. YAML uses hyphens-followed by a space, on a new indented line. - Strings: JSON requires quotes around all strings. YAML only requires quotes if the string contains special characters or could be misinterpreted as another type.
- Comments: JSON does not support comments. YAML supports comments using the
#symbol. When converting YAML to JSON, comments are stripped. - Multi-line Strings: YAML supports block scalars (
|and>) for multi-line text. JSON does not; newlines must be escaped as\n.
This tool uses js-yaml, a mature, battle-tested parser that handles these differences correctly. It preserves data types, correctly interprets block scalars, and resolves YAML anchors and aliases.
The Most Common YAML Error: Indentation
The single most frequent source of frustration with YAML is indentation. YAML does not use braces to define scope; it uses spaces. If you accidentally use a tab instead of spaces, or if you misalign a nested key by even one space, the parser will throw an error or, worse, silently produce the wrong structure.
This is where a converter becomes a debugging tool. When your YAML fails to parse, this tool will display the exact line number and character position of the error, along with a description of what went wrong. For example, it will tell you if you have a "bad indentation of a mapping entry" or if you are missing a colon. Seeing the error explicitly is often the fastest way to fix it.
Understanding YAML Anchors and Aliases
One of YAML's most powerful features is anchors (&) and aliases (*). Anchors allow you to define a block of data once and reuse it multiple times. This is heavily used in Kubernetes and CI/CD pipelines to avoid duplication.
For example: defaults: &default-settings {timeout: 30, retries: 3}. Later, you can reference it with service1: *default-settings. JSON has no equivalent feature. When you convert YAML to JSON, the converter must expand all aliases into their full values. This tool handles this expansion automatically, producing a fully self-contained JSON document.
How to Use This Tool
- Select the Direction: Choose "YAML → JSON" or "JSON → YAML" using the tabs.
- Paste Your Data: Insert your YAML or JSON into the input textarea.
- Convert: Click the conversion button. The tool parses the input and generates the output format.
- Review Errors: If the input is invalid, the tool will display a clear error message with the line number. Fix the error and try again.
- Copy: Use the "Copy" button to transfer the converted output to your editor or clipboard.
Frequently Asked Questions
Is my YAML or JSON data sent to a server?
No. This tool runs entirely in your browser using client-side JavaScript and the js-yaml library. Your data never leaves your device and is not logged or stored anywhere. This makes it safe for sensitive configuration files.
Why am I getting a "bad indentation" error?
YAML relies on precise spacing. This error means a line is indented incorrectly relative to its parent. Ensure you are using spaces, not tabs, and that nested keys are indented consistently (usually by 2 or 4 spaces). The tool will tell you exactly which line is causing the issue.
Does YAML support comments?
Yes. YAML uses the # symbol for comments. However, when you convert YAML to JSON, all comments are stripped because JSON does not support them.
How are multi-line strings handled?
YAML supports block scalars using | (preserve newlines) and > (fold newlines into spaces). When converting to JSON, these are correctly transformed into standard JSON strings with \n escapes where necessary.
What are YAML anchors and aliases?
Anchors (&) and aliases (*) allow you to reuse blocks of configuration. When converting to JSON, the tool automatically expands all aliases into their full values, because JSON does not have a reference mechanism.
Why did my number convert to a string?
YAML's implicit typing can sometimes treat a number like 1.0 as a float or a string depending on context. If you need strict type control, wrap the value in quotes in your YAML (e.g., version: "1.0"). The converter respects these quotes.
Can this tool handle Docker Compose or Kubernetes files?
Yes. This tool is perfect for converting docker-compose.yml or Kubernetes manifests into JSON for debugging or API submission. It handles all standard YAML structures used by these tools.
Is there a file size limit?
The limit depends on your browser's memory. Most modern browsers can handle YAML files of several megabytes without issue. For extremely large files (>10MB), a command-line tool like yq might be faster.
Why are my keys not in the same order as my YAML?
JSON objects are inherently unordered according to the specification. While most modern parsers preserve insertion order, there is no guarantee. If order is critical, you should not rely on JSON object key order.
What is the difference between YAML and YML?
They are the same thing. .yml is a shorter file extension for YAML. Both are parsed identically.
Does this tool support the YAML 1.2 specification?
Yes. The js-yaml library used by this tool follows the YAML 1.2 specification, which is the most widely used version today.
How do I represent null in YAML?
You can use null, ~, or simply leave the value blank after the colon. All are parsed as null and converted to null in JSON.
Can I convert JSON to YAML as well?
Yes. Use the "JSON → YAML" tab. The tool will parse the JSON and output a clean, properly indented YAML document.
Why does my YAML have a --- at the top?
--- is the YAML document separator. It is optional but often used to denote the start of a document. This tool accepts it in the input but does not add it to the output unless necessary.
How do I handle special characters in YAML?
Strings containing special characters like :, {, }, [, ], or # must be wrapped in quotes. The parser will throw an error if these are unquoted and ambiguous.
Does this tool work offline?
After the page loads, the tool works offline because all parsing runs in your browser.
What is a YAML sequence?
A sequence is YAML's version of an array. It is represented by a hyphen followed by a space, with each item on a new line, indented to the same level.
Why is my boolean value converted to a string?
YAML 1.1 treats yes, no, on, and off as booleans. YAML 1.2 (which this tool uses) treats them as strings unless quoted. If you need a boolean, use true or false.
How do I escape a colon in YAML?
If a string contains a colon followed by a space, it must be wrapped in quotes. For example: key: "value: with colon".
What is the difference between | and > in YAML?
| (literal block) preserves newlines exactly as written. > (folded block) folds newlines into spaces, creating a single long line. This is useful for long paragraphs.
Can I use tabs in YAML?
No. The YAML specification strictly forbids tabs for indentation. You must use spaces. If you paste YAML with tabs, the tool will throw an error.
Why is my JSON output minified?
This tool pretty-prints JSON with standard 2-space indentation. If your output looks minified, check that your input did not contain any parsing errors that caused it to fall back to raw output.
No comments:
Post a Comment