YAML syntax for manifests
Read a configuration at a glance
YAML is a text format for representing data. Kubernetes commonly uses it to describe resources, although its API accepts JSON representations as well. A manifest is a file describing one or more objects. Before thinking about what Kubernetes will do, learn to read the structure of the data. The following is ordinary YAML, not a Kubernetes resource.
catalog: enabled: true replicas: 2 version: "2" sections: - books - magazinesThe outer mapping contains catalog. Its value is another mapping. sections holds two ordered items. A reader can understand this file without a running cluster.
Mappings, sequences and scalar values
A mapping associates keys with values, such as replicas: 2. A sequence is an ordered collection, usually written with a dash before each item. A scalar is a single value such as a string, integer or boolean. A value can itself be another mapping or sequence. Use indentation with spaces to express nesting. Two spaces per level is a readable convention, not a YAML requirement. Siblings at the same nesting level must align.
The same data in JSON
JSON uses braces for objects, brackets for arrays, commas between entries and double quotes for string keys. Here is the equivalent data.
{ "catalog": { "enabled": true, "replicas": 2, "version": "2", "sections": [ "books", "magazines" ] }}The number 2 and string "2" are different values in either format. true is a boolean, while "true" is text. Choose the type the receiving API requires. YAML parsers may use different scalar-resolution rules; quote ambiguous strings such as yes, on and numeric-looking identifiers. The authoring parser in this batch is PyYAML 6.0.3, which is not the Kubernetes conversion implementation.
Fix a broken sequence
The following fragment is intentionally invalid. The second dash is not aligned with the first.
containers: - name: catalog image: example.invalid/catalog:1 - name: helperCorrect it by aligning both dashes and the fields belonging to each item:
containers: - name: catalog image: example.invalid/catalog:1 - name: helper image: example.invalid/helper:1There are now two mappings inside the list. These are illustrative image references under the reserved .invalid domain; do not try to pull them. The fragment is not a standalone resource.
Comments and multiple documents
Outside quoted strings, a properly separated # introduces a comment. JSON has no standard comment syntax. A YAML stream can hold multiple documents separated by --- on its own line. For example:
name: catalog # first record---name: reportsA reader supporting multiple documents sees two separate mappings. Do not confuse this with a list of two items in one document. Anchors and aliases can reuse YAML nodes, but parser and tool behavior can differ. Keep initial Kubernetes manifests explicit and do not assume every YAML feature has a portable JSON or Kubernetes equivalent.
Practice and next step
Read the sample catalog data aloud as a tree, identify the types, and predict what changes when the version loses its quotation marks. In the next lesson you will use these structures to read a complete Kubernetes object. Related reference: YAML syntax for manifests.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
YAML syntax for manifests — knowledge check
Apply the lesson concepts to original offline scenarios.
What kind of value is `sections` in the catalog example?
Which YAML value explicitly represents a string containing the digit 2?
What fixes the misaligned second list item?
What does a standalone `---` between two YAML documents do?
Why might a correctly parsed YAML file still be unusable by Kubernetes?