y
y
yq
Search…
Multiply (Merge)

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
  • + append arrays
  • d deeply merge arrays
  • ? only merge existing fields
  • n only merge new fields

Merge two files together

This uses the load operator to merge file2 into file1.
1
yq '. *= load("file2.yml")' file1.yml
Copied!

Merging all files

Note the use of eval-all to ensure all documents are loaded into memory.
1
yq eval-all '. as $item ireduce ({}; . * $item )' *.yml
Copied!

Merging complex arrays together by a key field

By default - yq merge is naive. It merges maps when they match the key name, and arrays are merged either by appending them together, or merging the entries by their position in the array.
For more complex array merging (e.g. merging items that match on a certain key) please see the example here
Note that versions prior to 4.18 require the 'eval/e' command to be specified.
yq e <exp> <file>

Multiply integers

Given a sample.yml file of:
1
a: 3
2
b: 4
Copied!
then
1
yq '.a *= .b' sample.yml
Copied!
will output
1
a: 12
2
b: 4
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 '.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 '. * {"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 '. * {"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 '. * {"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 '.a *? .b' sample.yml
Copied!
will output
1
thing: two
2
cat: frog
Copied!

Merge, only new 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 '.a *n .b' sample.yml
Copied!
will output
1
thing: one
2
cat: frog
3
missing: two
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 '.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 '.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 '.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

This is a fairly complex expression - you can use it as is by providing the environment variables as seen in the example below.
It merges in the array provided in the second file into the first - matching on equal keys.
Explanation:
The approach, at a high level, is to reduce into a merged map (keyed by the unique key) and then convert that back into an array.
First the expression will create a map from the arrays keyed by the idPath, the unique field we want to merge by. The reduce operator is merging '({}; . * $item )', so array elements with the matching key will be merged together.
Next, we convert the map back to an array, using reduce again, concatenating all the map values together.
Finally, we set the result of the merged array back into the first doc.
Thanks Kev from stackoverflow
Given a sample.yml file of:
1
myArray:
2
- a: apple
3
b: appleB
4
- a: kiwi
5
b: kiwiB
6
- a: banana
7
b: bananaB
8
something: else
Copied!
And another sample another.yml file of:
1
newArray:
2
- a: banana
3
c: bananaC
4
- a: apple
5
b: appleB2
6
- a: dingo
7
c: dingoC
Copied!
then
1
idPath=".a" originalPath=".myArray" otherPath=".newArray" yq eval-all '
2
(
3
(( (eval(strenv(originalPath)) + eval(strenv(otherPath))) | .[] | {(eval(strenv(idPath))): .}) as $item ireduce ({}; . * $item )) as $uniqueMap
4
| ( $uniqueMap | to_entries | .[]) as $item ireduce([]; . + $item.value)
5
) as $mergedArray
6
| select(fi == 0) | (eval(strenv(originalPath))) = $mergedArray
7
' sample.yml another.yml
Copied!
will output
1
myArray:
2
- a: apple
3
b: appleB2
4
- a: kiwi
5
b: kiwiB
6
- a: banana
7
b: bananaB
8
c: bananaC
9
- a: dingo
10
c: dingoC
11
something: else
Copied!

Merge to prefix an element

Given a sample.yml file of:
1
a: cat
2
b: dog
Copied!
then
1
yq '. * {"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 '.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 '.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 '.foobar * .foobarList' sample.yml
Copied!
will output
1
c: foobarList_c
2
!!merge <<:
3
- *foo
4
- *bar
5
thing: foobar_thing
6
b: foobarList_b
Copied!

Custom types: that are really numbers

When custom tags are encountered, yq will try to decode the underlying type.
Given a sample.yml file of:
1
a: !horse 2
2
b: !goat 3
Copied!
then
1
yq '.a = .a * .b' sample.yml
Copied!
will output
1
a: !horse 6
2
b: !goat 3
Copied!

Custom types: that are really maps

Custom tags will be maintained.
Given a sample.yml file of:
1
a: !horse
2
cat: meow
3
b: !goat
4
dog: woof
Copied!
then
1
yq '.a = .a * .b' sample.yml
Copied!
will output
1
a: !horse
2
cat: meow
3
dog: woof
4
b: !goat
5
dog: woof
Copied!
Last modified 1mo ago