How to merge two .yaml files such that shared keys between the files uses only one of their values? #1561
-
Hi, I am attempting to merge two yaml files and would like any shared keys under a specific key to use values from one of the yaml files, and not merge both. This problem may be better described using an example. GIven file1.yaml and file2.yaml, I am trying to achieve the following: file1.yaml
file2.yaml
My ideal result in merging is the following file: file3.yaml
Specifically, I would like to overwrite any key under paths such that if both yaml files have the same key under paths, then only use the value from file2. Is this possible? Apologies in advance for the convoluted description of the problem |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You may need to modify this a little, but hopefully you get the gist: yq '. as $d1 | load("file2.yaml") as $d2 |
($d1 | .paths | keys | .[]) as $i ireduce($d2;
.paths[$i] = .paths[$i] // $d1.paths[$i])' file1.yaml Explanation:
Hope that makes sense! |
Beta Was this translation helpful? Give feedback.
You may need to modify this a little, but hopefully you get the gist:
Explanation:
High level, this works by reducing the path keys from file1 into file2, where each path we either use file2 if present, file1 otherwise.
'. as $d1 | load("file2.yaml") as $d2
Load the files into $d1 and $d1($d1 | .paths | keys | .[]) as $i ireduce($d2
Reduce the paths in $d1 into $d2.paths[$i] = .paths[$i] // $d1.paths[$i]
Every path element in $d1, update the path entry in $d2. Using the alternative operator//
this will use the existing $d2 path if it's…