y
y
yq
Search…
Multiply (Merge)
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 and arrays - merging

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.

Merge Flags

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

Merging files

Note the use of eval-all to ensure all documents are loaded into memory.
1
yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' file1.yaml file2.yaml
Copied!

Multiply integers

Running
1
yq eval --null-input '3 * 4'
Copied!
will output
1
12
Copied!

Merge objects together, returning merged result only

Given a sample.yml file of:
1
a:
2
field: me
3
fieldA: cat
4
b:
5
field:
6
g: wizz
7
fieldB: dog
Copied!
then
1
yq eval '.a * .b' sample.yml
Copied!
will output
1
field:
2
g: wizz
3
fieldA: cat
4
fieldB: dog
Copied!

Merge objects together, returning parent object

Given a sample.yml file of:
1
a:
2
field: me
3
fieldA: cat
4
b:
5
field:
6
g: wizz
7
fieldB: dog
Copied!
then
1
yq eval '. * {"a":.b}' sample.yml
Copied!
will output
1
a:
2
field:
3
g: wizz
4
fieldA: cat
5
fieldB: dog
6
b:
7
field:
8
g: wizz
9
fieldB: dog
Copied!

Merge keeps style of LHS

Given a sample.yml file of:
1
a: {things: great}
2
b:
3
also: "me"
Copied!
then
1
yq eval '. * {"a":.b}' sample.yml
Copied!
will output
1
a: {things: great, also: "me"}
2
b:
3
also: "me"
Copied!

Merge arrays

Given a sample.yml file of:
1
a:
2
- 1
3
- 2
4
- 3
5
b:
6
- 3
7
- 4
8
- 5
Copied!
then
1
yq eval '. * {"a":.b}' sample.yml
Copied!
will output
1
a:
2
- 3
3
- 4
4
- 5
5
b:
6
- 3
7
- 4
8
- 5
Copied!

Merge, only existing fields

Given a sample.yml file of:
1
a:
2
thing: one
3
cat: frog
4
b:
5
missing: two
6
thing: two
Copied!
then
1
yq eval '.a *? .b' sample.yml
Copied!
will output
1
thing: two
2
cat: frog
Copied!

Merge, appending arrays

Given a sample.yml file of:
1
a:
2
array:
3
- 1
4
- 2
5
- animal: dog
6
value: coconut
7
b:
8
array:
9
- 3
10
- 4
11
- animal: cat
12
value: banana
Copied!
then
1
yq eval '.a *+ .b' sample.yml
Copied!
will output
1
array:
2
- 1
3
- 2
4
- animal: dog
5
- 3
6
- 4
7
- animal: cat
8
value: banana
Copied!

Merge, only existing fields, appending arrays

Given a sample.yml file of:
1
a:
2
thing:
3
- 1
4
- 2
5
b:
6
thing:
7
- 3
8
- 4
9
another:
10
- 1
Copied!
then
1
yq eval '.a *?+ .b' sample.yml
Copied!
will output
1
thing:
2
- 1
3
- 2
4
- 3
5
- 4
Copied!

Merge, deeply merging arrays

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:
1
a:
2
- name: fred
3
age: 12
4
- name: bob
5
age: 32
6
b:
7
- name: fred
8
age: 34
Copied!
then
1
yq eval '.a *d .b' sample.yml
Copied!
will output
1
- name: fred
2
age: 34
3
- name: bob
4
age: 32
Copied!

Merge arrays of objects together, matching on a key

It's a complex command, the trickyness comes from needing to have the right context in the expressions. First we save the second array into a variable '$two' which lets us reference it later. We then need to update the first array. We will use the relative update (|=) because we need to update relative to the current element of the array in the LHS in the RHS expression. We set the current element of the first array as $cur. Now we multiply (merge) $cur with the matching entry in $two, by passing $two through a select filter.
Given a sample.yml file of:
1
- a: apple
2
b: appleB
3
- a: kiwi
4
b: kiwiB
5
- a: banana
6
b: bananaB
Copied!
And another sample another.yml file of:
1
- a: banana
2
c: bananaC
3
- a: apple
4
b: appleB2
5
- a: dingo
6
c: dingoC
Copied!
then
1
yq eval-all '(select(fi==1) | .[]) as $two | select(fi==0) | .[] |= (. as $cur | $cur * ($two | select(.a == $cur.a)))' sample.yml another.yml
Copied!
will output
1
- a: apple
2
b: appleB2
3
- a: kiwi
4
b: kiwiB
5
- a: banana
6
b: bananaB
7
c: bananaC
Copied!

Merge to prefix an element

Given a sample.yml file of:
1
a: cat
2
b: dog
Copied!
then
1
yq eval '. * {"a": {"c": .a}}' sample.yml
Copied!
will output
1
a:
2
c: cat
3
b: dog
Copied!

Merge with simple aliases

Given a sample.yml file of:
1
a: &cat
2
c: frog
3
b:
4
f: *cat
5
c:
6
g: thongs
Copied!
then
1
yq eval '.c * .b' sample.yml
Copied!
will output
1
g: thongs
2
f: *cat
Copied!

Merge copies anchor names

Given a sample.yml file of:
1
a:
2
c: &cat frog
3
b:
4
f: *cat
5
c:
6
g: thongs
Copied!
then
1
yq eval '.c * .a' sample.yml
Copied!
will output
1
g: thongs
2
c: &cat frog
Copied!

Merge with merge anchors

Given a sample.yml file of:
1
foo: &foo
2
a: foo_a
3
thing: foo_thing
4
c: foo_c
5
bar: &bar
6
b: bar_b
7
thing: bar_thing
8
c: bar_c
9
foobarList:
10
b: foobarList_b
11
!!merge <<:
12
- *foo
13
- *bar
14
c: foobarList_c
15
foobar:
16
c: foobar_c
17
!!merge <<: *foo
18
thing: foobar_thing
Copied!
then
1
yq eval '.foobar * .foobarList' sample.yml
Copied!
will output
1
c: foobarList_c
2
<<:
3
- *foo
4
- *bar
5
thing: foobar_thing
6
b: foobarList_b
Copied!