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

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, 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 eval '.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 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

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 '.a', 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.
To use this, you will need to update '.myArray' in the expression to your array (e.g. .my.array), and '.a' to be the key field of your array (e.g. '.name')
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
myArray:
2
- a: banana
3
c: bananaC
4
- a: apple
5
b: appleB2
6
- a: dingo
7
c: dingoC
Copied!
then
1
yq eval-all '
2
(
3
((.myArray[] | {.a: .}) as $item ireduce ({}; . * $item )) as $uniqueMap
4
| ( $uniqueMap | to_entries | .[]) as $item ireduce([]; . + $item.value)
5
) as $mergedArray
6
| select(fi == 0) | .myArray = $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 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
!!merge <<:
3
- *foo
4
- *bar
5
thing: foobar_thing
6
b: foobarList_b
Copied!
Last modified 4d ago