Why indentation changes structure in configuration files: YAML

プログラミング・Web開発カテゴリを表すパンダのイラスト Programming / Web Development

About this article
This article was created using an automated generation workflow utilizing generative AI. Based on official documentation and fixed course code, operations, observations, and limitations were organized.

Verification status: Partial comparison with official information and implementation. Live browser behavior on fixed versions has not been verified.

In YAML, whitespace preceding a key determines parent-child relationships. Remove a single space and see where the same name moves.

Open live demo

Terms to know first

  • YAMLis a text format that allows settings to be written in hierarchies.

  • Indentationis the hierarchy indicated by leading whitespace on a line.

Thinking with a mind map

Read the left and right cases from the center. Adding whitespace to the beginning of port changes its affiliation.

mindmap
    root((設定の階層))
      インデントあり
        server の中に port
      インデントなし
        server と port が同じ階層

Live demo operations

Indent the beginning of port by two spaces. Then, remove the whitespace.

How to read the results

The same key moves to a different hierarchy.

Removing the whitespace leaves the server value empty, and port moves to the same hierarchy. It is important to note that the meaning of the configuration changes even when there are no errors.

Key code points

Display the result of parseDocument as JSON and check the position if there are any errors.

The implementation can be viewed in the "Implementation Code" section of the demo screen.

When using it for work

Review configuration changes not only after formatting, but also in the converted structure.

Compare structures after removing whitespace

The initial input consists of a line reading "server:" followed by two spaces and "port: 8080". When executed, we expect an object to be displayed where "port" is nested under the key "server". Next, run it with only the two leading spaces before "port" removed. This time, you can expect "server" and "port" to be on the same level, with the value of "server" becoming null. Verify that the configurations are not identical even if no errors are generated.

ConditionExpected structure
Two spaces before port8080 for server.port
No spaces before portserver is null, 8080 for root port

When loading these two types of strings with local PyYAML 6.0.3, the structural differences shown in the table were confirmed. Since this is a reference test using a different parser, it should be distinguished from the actual measurement results of yaml 2.7.0. It is important to check the key positions and value types in the demo output.

Indentation is not decoration

YAML is sometimes used for configuration files read and written by humans. Leading spaces are meaningful for representing hierarchy. While JSON allows you to check nested objects by the position of braces, YAML requires you to read the structure from the position of the beginning of the line. If your editor hides whitespace, you might miss a single line belonging to a different hierarchy. Two spaces are a convention for this example and do not mean that all YAML files must use exactly two spaces. Maintain consistency within the same hierarchy and match your organization's style.

Furthermore, avoid using tabs for indentation. The visual whitespace width of tabs varies depending on editor settings, and they are not appropriate for YAML indentation. Using editor settings that make whitespace visible makes verification easier. Multi-line strings and similar constructs have different syntax rules, so simply removing whitespace from all lines and formatting them is a dangerous operation.

Processing performed by the parser

In the tutorial, parseDocument reads the input, and if there are elements in errors, it displays their messages. If there are no errors, it converts the value to JavaScript using toJS and displays it. This allows you to inspect how the input text is actually interpreted as a structure. Note that Document also contains warnings, but this implementation does not include warnings in the results. The absence of errors does not mean all potential issues are gone.

Even with the same content, the interpretation of strings, booleans, and other values may differ depending on the YAML version or schema. Verify which parser and settings the application handling the actual configuration uses. While this example is suitable for simple hierarchical comparisons, do not generalize type interpretation or duplicate key handling to all YAML processors.

Safely verifying configuration changes

Consider, for example, a design where a service references the value of server.port, and the port property moves to the root level. Even if the configuration file itself is syntactically valid, it may fail to satisfy the structure expected by the service. The behavior when configuration values are ignored, defaults are used, or startup fails varies depending on the service. In addition to validating whether the YAML can be parsed, verify through schemas or tests that required keys exist at the specified hierarchy.

During operations, compare the files before and after the change, and check the transformed structure before applying it. Retain approved configurations so they can be rolled back if issues occur. Demos should include only dummy ports and similar placeholders; do not paste actual credentials or internal hostnames. To distinguish between module loading errors and YAML errors, it is advisable to first revert to the original short example and verify that it works.

Validation Scope

The course-specified version is yaml 2.7.0. Reference tests were conducted using Python PyYAML 6.0.3 safe_load. Verified that server:\n port: 8080 parses to {'server':{'port':8080}}, and server:\nport: 8080 parses to {'server':None,'port':8080}. The yaml 2.7.0 core itself was not executed. CDN retrieval, pinned license version verification, and actual rendering are checked separately.

References

GitHub Samples

Implementation and Experimentation Procedures

Document information

Article title
Why indentation changes structure in configuration files: YAML
Published
Updated
Source
https://papanda925.com/?p=19095&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL