YAML Format
Learn YAML syntax for configuring your mocks in moclojer. Complete guide with practical examples and best practices.
YAML is the most common configuration format in moclojer. It's simple to read and write, requires no programming knowledge, and is perfect for defining mock APIs declaratively.
Why YAML?
Advantages:
β Readable: Looks like English, easy to understand
β Simple: Less verbose than JSON or XML
β Structured: Maintains clear hierarchy
β Comments: Can document inline
When to use YAML:
You're starting with moclojer
Need simple and clear configuration
Want to collaborate with non-programmers
Prefer readable configuration files
Basic Structure
Every moclojer YAML file is a list of endpoints:
- endpoint:
method: GET
path: /hello
response:
status: 200
body: "Hello, World!"
- endpoint:
method: POST
path: /users
response:
status: 201
body: '{"id": 1, "name": "Alice"}'Anatomy:
Each endpoint starts with
- endpoint:Indentation with 2 spaces (not tabs!)
Required keys:
path,responseOptional but recommended key:
method
Essential YAML Syntax
1. Indentation
Indentation defines hierarchy:
β οΈ IMPORTANT:
Always use 2 spaces per level
Never mix spaces and tabs
Tools: configure your editor for "soft tabs"
2. Strings
Three ways to write strings:
For JSON in body, use >:
3. Numbers and Booleans
4. Lists
5. Objects (Maps)
6. Comments
Complete Annotated Example
Best Practices
β
Do
Use consistent indentation (2 spaces)
Add explanatory comments
Use
>for inline JSONGroup related endpoints
Order by method and path
β Avoid
Tabs for indentation
Unnecessary quotes
JSON without
>Endpoints without comments in large files
YAML Troubleshooting
Problem: "YAML parse error: mapping values are not allowed"
Cause: Colon without space or quotes
Problem: "YAML parse error: did not find expected key"
Cause: Incorrect indentation
Problem: Broken JSON in body
Cause: Didn't use > for multi-line
YAML Validation
Online
YAML Lint - validates syntax
YAML to JSON - see how it will be parsed
Editors
VS Code: "YAML" extension by Red Hat
Sublime: "YAML Nav" extension
Vim: "vim-yaml" plugin
Command line
Comparison with Other Formats
YAML vs JSON
YAML wins in:
Readability (50% fewer characters)
Native comments
Multi-line strings
JSON wins in:
Faster parsing
Universal support
YAML vs EDN
For most users, YAML is simpler. Use EDN only if you:
Work with Clojure
Need complex data structures
Want programmatic integration
See EDN Format Guide for details.
Next Steps
Now that you've mastered YAML, explore:
Path Parameters - Dynamic parameters in URLs
Template Variables - Dynamic responses
Configuration Spec - Complete reference
See Also
OpenAPI Format - Import OpenAPI specs
Postman Format - Use Postman Collections
YAML Specification - Official YAML 1.2 spec
Last updated
Was this helpful?