Use Cases
- Converting Kubernetes manifests to JSON for programmatic processing
- Transforming CI/CD configs (GitHub Actions, GitLab CI) to JSON
- Parsing YAML config files for API consumption
- Validating YAML syntax by converting to JSON
- Processing multi-document YAML (e.g., Kubernetes resources with
---separators) - Data interchange between YAML-based and JSON-based systems
How It Works
The tool uses thejs-yaml library to parse YAML syntax and convert it to JavaScript objects, then serializes to JSON:
- Parse YAML using
yamlLoadAll()(supports multi-document) - Detect document count:
- Single document → Output single JSON object
- Multiple documents → Output JSON array of objects
- Serialize to JSON with 2-space indentation
The tool automatically detects multi-document YAML (documents separated by
---) and outputs a JSON array when multiple documents are found.Input Format
Single-Document YAML
Multi-Document YAML
Output Format
Single Document → JSON Object
Multiple Documents → JSON Array
Examples
Technical Details
Located in
lib/tools/engine.ts:497-501js-yaml (YAML 1.2 parser):
YAML Features Supported
- Scalars: strings, numbers, booleans, null
- Collections: sequences (arrays), mappings (objects)
- Multi-line strings: literal (
|) and folded (>) blocks - Anchors and aliases:
&anchorand*aliasreferences - Comments: Parsed but not preserved in JSON output
- Multi-document files: Separated by
---
Type Conversion
| YAML | JSON |
|---|---|
true, false, yes, no | true, false |
123, 0x1A, 0o17 | 123 (decimal) |
1.23, 1.23e4, .inf | 1.23, 12300, null |
null, ~ | null |
"quoted", unquoted | "string" |
2024-03-04 | "2024-03-04" (string) |
YAML Syntax Tips
Preserving Numeric Strings
Quote numbers that should remain strings:Multi-line Strings
Anchors and Aliases
Common Errors
YAML parse error: bad indentation
YAML parse error: bad indentation
YAML is indentation-sensitive (like Python). Use consistent spaces (2 or 4) for indentation. Do not mix tabs and spaces.
Unexpected key-value pair
Unexpected key-value pair
Ensure colons (
:) are followed by a space:Multi-document separator not recognized
Multi-document separator not recognized
The separator
--- must be on its own line:Related Tools
- JSON to YAML - Reverse conversion (JSON → YAML)
- JSON Format/Validate - Validate and format JSON output
- JSON to CSV - Convert JSON arrays to CSV