What does the YAML Formatter do?
The YAML Formatter & Prettifier on blueutils.com formats, cleans, and standardizes raw YAML (YAML Ain't Markup Language) text. It parses documents against YAML 1.2 specifications, cleans irregular whitespace, aligns key-value pairs, formats nested mappings and sequence arrays, and supports configurable indentation levels (2 spaces, 4 spaces, or custom indentation widths).
Core Concepts
Understanding YAML formatting and hierarchy rules ensures error-free manifests:
- Strict Space Indentation: Indentation in YAML must strictly use space characters (never tab stops
\t). Consistent 2-space or 4-space levels ensure cross-platform compatibility across Kubernetes, Docker Compose, Ansible, and GitHub Actions. - Hierarchy Alignment: Normalizes nested dictionaries and lists under standard block mapping notation for clear visual hierarchy.
- Comment & Quoting Preservation: Preserves code comments and standardizes necessary quotes around strings containing reserved punctuation (
:,#,{},[]). - Multi-Document Streams: Handles multiple YAML documents within a single stream separated by document start markers (
---).
How to use the tool?
- Paste or Upload YAML: Paste your YAML configuration, Docker Compose file, or Kubernetes manifest into the Raw YAML Input editor, upload a
.yamlfile, or click Sample. - Select Indentation: Choose your preferred indentation spacing from the toolbar dropdown (2 Spaces, 4 Spaces, or enter a custom indentation width).
- Instant Formatting & Export: The formatted result updates automatically in real time in the Formatted YAML Result pane. Click Copy to copy the formatted text to your clipboard or Download to save your clean
output.yamlfile.
Related Developer Utilities
If you work with YAML manifests, syntax validation, and data conversions, explore these related tools:
- YAML Syntax Validator: Check raw YAML files for syntax errors and line/column positions.
- YAML Minifier & Compressor: Compress YAML documents into compact flow-style representations.
- YAML to JSON Converter: Convert YAML manifests into standard JSON payloads.
- YAML to CSV Converter: Convert YAML sequences into tabular CSV spreadsheets.
- YAML Diff Comparator: Compare two YAML documents side-by-side to highlight structural and key-value differences.
REST API Integration
blueutils.com provides a free REST API endpoint (POST https://blueutils.com/api/yaml/format) to programmatically format and prettify raw YAML documents with custom indentation spacing.
API Request Parameters
| Name | Type | Description | Example |
|---|---|---|---|
rawText / yaml |
String / Object | Raw unformatted YAML payload string or parsed object to format. | "version: \"3.8\"\nservices:\n web:\n image: node:18-alpine" |
indent |
Number | Indentation spaces per level (default: 2). |
2 |
API Request Payload Examples
cURL (Using Raw String)
curl -X POST https://blueutils.com/api/yaml/format \
-H "Content-Type: application/json" \
-d '{
"rawText": "version: \"3.8\"\nservices:\n web:\n image: node:18-alpine",
"indent": 2
}'cURL (Using Direct Object)
curl -X POST https://blueutils.com/api/yaml/format \
-H "Content-Type: application/json" \
-d '{
"yaml": {
"version": "3.8",
"services": {
"web": { "image": "node:18-alpine" }
}
},
"indent": 2
}'Python
import requests
url = "https://blueutils.com/api/yaml/format"
payload = {
"rawText": "version: \"3.8\"\nservices:\n web:\n image: node:18-alpine",
"indent": 2
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.json())Java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
String jsonPayload = """
{
"rawText": "version: \\"3.8\\"\\nservices:\\n web:\\n image: node:18-alpine",
"indent": 2
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://blueutils.com/api/yaml/format"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}API Response Parameters
| Name | Type | Description | Example |
|---|---|---|---|
isValid |
Boolean | Indicates whether the input YAML parsed and formatted successfully. | true |
message |
String | Success confirmation message. | "YAML formatted successfully." |
result |
String | Prettified and standardized YAML output string. | "version: \"3.8\"\nservices:\n web:\n image: node:18-alpine\n" |
data |
Object / Array | Parsed native object/array representation returned when input is valid YAML. | {"version":"3.8"} |
originalSize |
Number | Byte size of raw input payload in UTF-8. | 58 |
resultSize |
Number | Byte size of formatted YAML output in UTF-8. | 64 |
API Response Payload Examples
Success Response (HTTP 200 OK)
{
"isValid": true,
"message": "YAML formatted successfully.",
"result": "version: \"3.8\"\nservices:\n web:\n image: node:18-alpine\n",
"data": {
"version": "3.8",
"services": {
"web": {
"image": "node:18-alpine"
}
}
},
"originalSize": 58,
"resultSize": 64
}Validation Failure Response (HTTP 400 Bad Request)
{
"isValid": false,
"error": "YAML syntax error (Line 3, Column 1): Unexpected scalar token",
"details": {
"line": 3,
"col": 1
}
}Rate Limit Exceeded Response (HTTP 429 Too Many Requests)
{
"error": "API rate limit exceeded. Please wait or contact support@blueutils.com."
}Why use an API to format YAML?
Integrating the YAML Formatter API into automated code linters, build pipelines, or developer platforms provides practical advantages:
- Rapid Script Validation: Standardizes indentation and syntax across user-contributed configuration files before repository commit.
- Optimized Token Efficiency for AI Agents: LLMs often generate misaligned YAML spaces. Invoking the formatter API ensures clean, valid YAML without consuming extra reasoning tokens.
- Deterministic Accuracy Without Hallucinations: Ensures 100% deterministic AST parsing and indentation rendering without dropping configuration keys.
Native Usage
How to format YAML files locally using code editors, terminal CLI utilities, and programming runtimes without external web services:
Visual Studio Code & JetBrains Shortcuts
- VS Code (Windows / Linux):
Shift + Alt + F(with Red Hat YAML extension installed) - VS Code (macOS):
Shift + Option + F - JetBrains IDEs (IntelliJ, PyCharm, WebStorm):
Ctrl + Alt + L(Windows/Linux) orCmd + Option + L(macOS) - Neovim:
:lua vim.lsp.buf.format()(withyamllslanguage server)
Windows (CMD / PowerShell)
# Format YAML using Python standard CLI in PowerShell
python -c "import yaml; print(yaml.dump(yaml.safe_load(open('config.yaml')), indent=2, sort_keys=False))" > formatted.yamlLinux / Unix (Bash & yq)
# Format YAML using the yq CLI utility
yq eval -P '.' config.yaml > formatted.yaml
# Format directly from standard input pipeline
cat manifest.yaml | yq eval -P '.' - > formatted.yamlPython
Format YAML using PyYAML in Python:
import yaml
with open('config.yaml', 'r', encoding='utf-8') as f:
data = yaml.safe_load(f)
formatted = yaml.dump(data, indent=2, sort_keys=False, default_flow_style=False)
print(formatted)Java
Format YAML using Jackson (YAMLMapper) in Java 17+:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import java.io.File;
public class YamlFormatterExample {
public static void main(String[] args) throws Exception {
YAMLMapper mapper = new YAMLMapper();
Object object = mapper.readValue(new File("config.yaml"), Object.class);
String formatted = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
System.out.println(formatted);
}
}