Like the multiple operator in jq, depending on the operands, this multiply operator will do different things. Currently numbers, arrays and objects are supported.
Objects are merged deeply matching on matching keys. By default, array values override and are not deeply merged.
Note that when merging objects, this operator returns the merged object (not the parent). This will be clearer in the examples below.
You can control how objects are merged by using one or more of the following flags. Multiple flags can be used together, e.g. .a *+? .b
. See examples below
+
to append arrays
?
to only merge existing fields
d
to deeply merge arrays
Note the use of eval-all
to ensure all documents are loaded into memory.
yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' file1.yaml file2.yaml
Running
yq eval --null-input '3 * 4'
will output
12
Given a sample.yml file of:
a:field: mefieldA: catb:field:g: wizzfieldB: dog
then
yq eval '.a * .b' sample.yml
will output
field:g: wizzfieldA: catfieldB: dog
Given a sample.yml file of:
a:field: mefieldA: catb:field:g: wizzfieldB: dog
then
yq eval '. * {"a":.b}' sample.yml
will output
a:field:g: wizzfieldA: catfieldB: dogb:field:g: wizzfieldB: dog
Given a sample.yml file of:
a: {things: great}b:also: "me"
then
yq eval '. * {"a":.b}' sample.yml
will output
a: {things: great, also: "me"}b:also: "me"
Given a sample.yml file of:
a:- 1- 2- 3b:- 3- 4- 5
then
yq eval '. * {"a":.b}' sample.yml
will output
a:- 3- 4- 5b:- 3- 4- 5
Given a sample.yml file of:
a:thing: onecat: frogb:missing: twothing: two
then
yq eval '.a *? .b' sample.yml
will output
thing: twocat: frog
Given a sample.yml file of:
a:array:- 1- 2- animal: dogvalue: coconutb:array:- 3- 4- animal: catvalue: banana
then
yq eval '.a *+ .b' sample.yml
will output
array:- 1- 2- animal: dog- 3- 4- animal: catvalue: banana
Given a sample.yml file of:
a:thing:- 1- 2b:thing:- 3- 4another:- 1
then
yq eval '.a *?+ .b' sample.yml
will output
thing:- 1- 2- 3- 4
Merging arrays deeply means arrays are merge like objects, with indexes as their key. In this case, we merge the first item in the array, and do nothing with the second.
Given a sample.yml file of:
a:- name: fredage: 12- name: bobage: 32b:- name: fredage: 34
then
yq eval '.a *d .b' sample.yml
will output
- name: fredage: 34- name: bobage: 32
Given a sample.yml file of:
a: catb: dog
then
yq eval '. * {"a": {"c": .a}}' sample.yml
will output
a:c: catb: dog
Given a sample.yml file of:
a: &catc: frogb:f: *catc:g: thongs
then
yq eval '.c * .b' sample.yml
will output
g: thongsf: *cat
Given a sample.yml file of:
a:c: &cat frogb:f: *catc:g: thongs
then
yq eval '.c * .a' sample.yml
will output
g: thongsc: &cat frog
Given a sample.yml file of:
foo: &fooa: foo_athing: foo_thingc: foo_cbar: &barb: bar_bthing: bar_thingc: bar_cfoobarList:b: foobarList_b!!merge <<:- *foo- *barc: foobarList_cfoobar:c: foobar_c!!merge <<: *foothing: foobar_thing
then
yq eval '.foobar * .foobarList' sample.yml
will output
c: foobarList_c<<:- *foo- *barthing: foobar_thingb: foobarList_b