How do I override an array to be empty in YAML

6k views Asked by At

Say I have a YML file original.yml with an array of objects

array_in_yml:
- start: 1
- middle: 2
- end: 3

I included it in modified.yml

!include "original.yml" 
array_in_yml: []

I am expecting this array to be empty when I load the modified.yml, but it seems to have 3 values as original.yml. How do I force/override the array to be empty?

1

There are 1 answers

1
flyx On

The discussion about !include seems to lead a bit away from the actual question. Let's assume that in some unknown way, the !include line gets replaced with the content in original.yml. We would have:

array_in_yml:
- start: 1
- middle: 2
- end: 3
array_in_yml: []

This is not valid YAML, since every key in a dictionary must be unique, but you use the key array_in_yml twice. Your YAML processor might ignore this and simply assign the first value (which is a sequence of three items) to the key array_in_yml.

Now the important part: There is no way in YAML to modify previously given values. You cannot override a value given previously with a different one. What you want to do is outside the YAML spec and you would need some merging tool that does such replacements for you.