Search code examples
yamlyq

Deep merge two YAML files and keep only new fields


I have following master.yaml file

root:
  - name: First
    some_value: One
    one_more: Four
    elements:
      first: e
  - name: Second
    some_value: Two
    elements:
      first: a
      second: b

and update.yaml file

defaults:
  other_value: true
  one_more: Three
  elements:
      first: a
      second: b
      third: c

The expected result is:

root:
  - name: First
    some_value: One
    other_value: true
    one_more: Four
    elements:
      first: e
      second: b
      third: c
  - name: Second
    some_value: Two
    other_value: true
    one_more: Three
    elements:
      first: a
      second: b
      third: c

I have already following query: yq '.root[] *= load(\""update.yaml\"").defaults' master.yaml

but it replaces existing values.


Solution

  • mikeafarh/yq supports merging only new fields, so that the existing ones are not modified

    yq '.root[] *=n load("update.yaml").defaults' master.yaml