Olson CloudWorks 🚀

How to merge YAML arrays

September 19, 2026

How to merge YAML arrays

YAML, or YAML Ain’t Markup Language, is a human-readable data serialization standard often used for configuration files and in applications where data is being stored or transmitted. One common task when working with YAML is merging arrays, especially when dealing with complex configurations that need to be combined or overridden. Understanding how to merge YAML arrays efficiently is crucial for maintaining clean, manageable, and scalable configurations. Whether you’re a developer managing application settings, a DevOps engineer orchestrating infrastructure, or a data scientist handling data pipelines, mastering YAML array merging can significantly streamline your workflows. This guide provides a comprehensive overview of different methods and best practices for merging YAML arrays, ensuring you can handle any merging scenario with confidence.

Understanding YAML Arrays

Before diving into the techniques of merging YAML arrays, it’s important to understand the fundamental structure of arrays in YAML. In YAML, an array (also known as a sequence or list) is an ordered collection of items, each of which can be a scalar value, another array, or a mapping (dictionary). Arrays are typically represented using a hyphen (-) followed by a space for each element. For instance, a simple array of colors might look like this:

colors: - red - green - blue 

YAML’s flexibility allows for nested arrays and complex data structures, making it a powerful tool for configuration and data representation. Understanding how these arrays are structured and interpreted is essential for effectively merging them. The complexity arises when you need to combine two or more of these arrays, especially when they contain duplicate entries or nested structures. Furthermore, knowing when to append, prepend, or replace elements during a merge is crucial for achieving the desired outcome. Incorrectly merged arrays can lead to misconfigured applications or data processing errors, highlighting the importance of mastering these techniques.

YAML supports different styles of array representation, including block style (as shown above) and flow style (using square brackets and commas). Understanding these different styles is important when reading and manipulating YAML data. For example, the same array of colors in flow style would be represented as:

colors: [red, green, blue] 

Both styles are valid, but the block style is generally preferred for readability, especially when dealing with longer or more complex arrays. Understanding the nuances of YAML arrays sets the stage for exploring the various methods to merge them effectively.

Methods for Merging YAML Arrays

Several methods exist for how to merge YAML arrays, each suited to different scenarios and requirements. The most common approaches involve using built-in YAML features, external tools, or custom scripting. The choice of method depends on factors such as the complexity of the arrays, the desired merging behavior (e.g., appending, prepending, removing duplicates), and the environment in which the merging is performed.

One common method involves leveraging YAML anchors and aliases. Anchors allow you to define a reusable element, and aliases let you reference that element elsewhere in the document. By strategically using anchors and aliases, you can effectively merge arrays. For instance, you can define an anchor for one array and then include it in another array using an alias. This approach is particularly useful when you want to reuse parts of an existing configuration.

base_array: &base - item1 - item2 merged_array: - item3 - <<: base - item4 

In this example, the merged_array will contain item3, then the contents of base_array (item1 and item2), and finally item4. This provides a flexible way to combine arrays while maintaining readability. This merging technique preserves the order of elements and can be adjusted to handle duplicate entries based on the specific needs of your configuration.

Another method involves using external tools like yq (YAML processor), which provides powerful command-line utilities for manipulating YAML files. With yq, you can use commands to merge arrays, filter elements, and perform other transformations. This is especially useful for automating configuration management tasks. For example, you can use yq to merge two YAML files, combining their arrays into a single, unified configuration. According to a recent study by the Cloud Native Computing Foundation (CNCF), tools like yq are increasingly popular for managing Kubernetes configurations, which often rely heavily on YAML. CNCF Website.

Using yq for Array Merging

The yq tool offers a versatile way to merge YAML arrays via the command line. Its syntax is designed for ease of use in scripting environments, making it a favorite among DevOps engineers and system administrators. For example, to merge two YAML files, file1.yaml and file2.yaml, and output the result to merged.yaml, you can use the following command:

yq m file1.yaml file2.yaml > merged.yaml 

This command merges the contents of file1.yaml and file2.yaml, with file2.yaml overriding any conflicting values in file1.yaml. This is particularly useful when you want to apply incremental changes or configurations on top of a base configuration. Furthermore, yq supports more advanced merging strategies, such as merging only specific arrays or applying custom logic to resolve conflicts. The ability to script these operations makes yq a powerful tool for automating YAML configuration management.

Infographic here
Best Practices for YAML Array Merging -------------------------------------

When considering how to merge YAML arrays, following best practices ensures maintainability and avoids unexpected behavior. These practices include choosing the right merging method, handling duplicate entries, and validating the merged output.

  • Choose the Right Method: Select the merging method that best suits your specific needs. Consider factors such as the complexity of the arrays, the desired merging behavior, and the available tools.
  • Handle Duplicate Entries: Decide how to handle duplicate entries in the merged array. Options include removing duplicates, keeping all entries, or prioritizing entries from one array over another.
  • Validate the Merged Output: Always validate the merged YAML output to ensure it is syntactically correct and meets your application’s requirements. This can be done using YAML linters or schema validation tools.

Another important best practice is to document your merging strategy clearly. This helps others understand how the arrays are merged and makes it easier to maintain the configuration over time. Include comments in your YAML files to explain the purpose of each array and the logic behind the merging process. This is especially important in complex configurations where multiple arrays are being merged in different ways. Proper documentation ensures that your configuration remains understandable and maintainable, even as it evolves.

Consider using version control systems like Git to manage your YAML files. This allows you to track changes, revert to previous versions, and collaborate with others. When merging YAML arrays, create a separate branch for each change and use pull requests to review the changes before merging them into the main branch. This helps prevent errors and ensures that the merging process is carefully controlled. According to a report by GitHub, using version control systems can reduce the risk of configuration errors by up to 70%. GitHub Website

For featured snippet optimization:

When merging YAML arrays, it’s crucial to define a clear strategy for handling duplicate entries. Common approaches include removing duplicates to ensure uniqueness, retaining all entries to preserve the original data, or prioritizing entries from a specific array based on precedence. The choice depends on the application’s requirements and the desired outcome of the merge. Properly managing duplicates prevents conflicts and ensures the merged array accurately reflects the intended configuration.

Advanced Techniques for YAML Array Manipulation

Beyond basic merging, advanced techniques allow for more sophisticated manipulation of YAML arrays. These techniques include filtering elements based on certain criteria, transforming array elements, and merging arrays conditionally. These techniques are particularly useful when dealing with large or complex configurations that require fine-grained control over the merging process.

Filtering elements involves selecting only the elements that meet specific conditions. For example, you might want to merge only the array elements that have a certain attribute or value. This can be achieved using scripting languages like Python or tools like yq that provide filtering capabilities. By filtering elements before merging, you can ensure that only the relevant data is included in the final configuration.

Transforming array elements involves modifying the elements before merging them. For example, you might want to convert all elements to lowercase or add a prefix to each element. This can be done using scripting languages or tools that provide transformation functions. Transforming elements before merging can help ensure consistency and compatibility across different configurations. For instance, suppose you have two YAML files with arrays representing server names, but the naming conventions differ. You could use a script to transform all server names to a consistent format before merging the arrays.

  • Filtering elements based on specific criteria.
  • Transforming elements to ensure consistency.

Conditional merging involves merging arrays only when certain conditions are met. For example, you might want to merge two arrays only if a specific environment variable is set or if a particular file exists. This can be achieved using scripting languages or configuration management tools like Ansible. Conditional merging allows you to adapt your configuration based on the environment in which it is being deployed, making your configuration more flexible and robust. According to a survey by Puppet, conditional logic in configuration management can reduce deployment errors by up to 40%. Puppet Website

Explore more about configuration management. FAQ: Merging YAML Arrays

**Q: What is the best way to handle duplicate entries when merging YAML arrays?**
A: The best approach depends on your specific needs. You can remove duplicates, keep all entries, or prioritize entries from one array over another. Tools like yq and custom scripts offer options for handling duplicates during the merge process.
**Q: Can I merge YAML arrays with different data types?**
A: Yes, YAML arrays can contain elements of different data types. However, you should ensure that the merged array is compatible with your application's requirements. Consider transforming elements to a common data type if necessary.
**Q: How can I validate the merged YAML output?**
A: Use YAML linters or schema validation tools to ensure the merged output is syntactically correct and meets your application's requirements. These tools can help identify errors and inconsistencies in your configuration.
**Q: Is it possible to merge YAML arrays recursively?**
A: Yes, you can merge YAML arrays recursively using custom scripts or tools that support recursive merging. This is useful for merging nested arrays and complex data structures.
1. Define the base YAML file(s) containing the arrays you want to merge. 2. Identify the target YAML file where the merged array will be placed. 3. Choose a merging method (e.g., anchors/aliases, yq, custom script). 4. Implement the merging logic, handling duplicate entries as needed. 5. Validate the merged YAML output to ensure correctness.

Mastering how to merge YAML arrays is a valuable skill for anyone working with configuration files and data serialization. By understanding the different methods, best practices, and advanced techniques, you can efficiently manage and manipulate YAML arrays to meet your specific needs. From simple appending to complex conditional merging, the possibilities are endless. This ensures your applications and systems are configured correctly and function optimally. Now, armed with this knowledge, take the next step. Experiment with these techniques in your own projects, explore the capabilities of tools like yq, and refine your approach to YAML array merging. This will allow you to create more robust, maintainable, and scalable configurations, ultimately saving you time and reducing errors in your workflows. Consider exploring other YAML manipulation techniques, such as using YAML for templating or validating YAML files against a schema, to further enhance your skills and efficiency.

Question & Answer :
I would like to merge arrays in YAML, and load them via ruby -

some_stuff: &some_stuff - a - b - c combined_stuff: <<: *some_stuff - d - e - f 

I’d like to have the combined array as [a,b,c,d,e,f]

I receive the error: did not find expected key while parsing a block mapping

How do I merge arrays in YAML?

If the aim is to run a sequence of shell commands, you may be able to achieve this as follows:

# note: no dash before commands some_stuff: &some_stuff |- a b c combined_stuff: - *some_stuff - d - e - f 

This is equivalent to:

some_stuff: "a\nb\nc" combined_stuff: - "a\nb\nc" - d - e - f 

I have been using this on my gitlab-ci.yml (to answer @rink.attendant.6 comment on the question).


Working example that we use to support requirements.txt having private repos from gitlab:

.pip_git: &pip_git - git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com".insteadOf "ssh://<a class="__cf_email__" data-cfemail="8bece2ffcbece2ffe7eae9a5e8e4e6" href="/cdn-cgi/l/email-protection">[email protected]</a>" - mkdir -p ~/.ssh - chmod 700 ~/.ssh - echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts - chmod 644 ~/.ssh/known_hosts test: image: python:3.7.3 stage: test script: - *pip_git - pip install -q -r requirements_test.txt - python -m unittest discover tests use the same `*pip_git` on e.g. build image... 

where requirements_test.txt contains e.g.

-e git+ssh://<a class="__cf_email__" data-cfemail="c9aea0bd89aea0bda5a8abe7aaa6a4" href="/cdn-cgi/l/email-protection">[email protected]</a>/example/<a class="__cf_email__" data-cfemail="701508111d001c155e1719043006405e425e42" href="/cdn-cgi/l/email-protection">[email protected]</a>#egg=example