Comment on page
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 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+
append arraysd
deeply merge arrays?
only merge existing fieldsn
only merge new fieldsc
clobber custom tags
This uses the load operator to merge file2 into file1.
yq '. *= load("file2.yml")' file1.yml
Note the use of
eval-all
to ensure all documents are loaded into memory.yq eval-all '. as $item ireduce ({}; . * $item )' *.yml
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
Given a sample.yml file of:
a: 3
b: 4
then
yq '.a *= .b' sample.yml
will output
a: 12
b: 4
Given a sample.yml file of:
a:
field: me
fieldA: cat
b:
field:
g: wizz
fieldB: dog
then
yq '.a * .b' sample.yml
will output
field:
g: wizz
fieldA: cat
fieldB: dog
Given a sample.yml file of:
a:
field: me
fieldA: cat
b:
field:
g: wizz
fieldB: dog
then
yq '. * {"a":.b}' sample.yml
will output
a:
field:
g: wizz
fieldA: cat
fieldB: dog
b:
field:
g: wizz
fieldB: dog
Given a sample.yml file of:
a: {things: great}
b:
also: "me"
then
yq '. * {"a":.b}' sample.yml
will output
a: {things: great, also: "me"}
b:
also: "me"
Given a sample.yml file of:
a:
- 1
- 2
- 3
b:
- 3
- 4
- 5
then
yq '. * {"a":.b}' sample.yml
will output
a:
- 3
- 4
- 5
b:
- 3
- 4
- 5
Given a sample.yml file of:
a:
thing: one
cat: frog
b:
missing: two
thing: two
then
yq '.a *? .b' sample.yml
will output
thing: two
cat: frog
Given a sample.yml file of:
a:
thing: one
cat: frog
b:
missing: two
thing: two
then
yq '.a *n .b' sample.yml
will output
thing: one
cat: frog
missing: two
Given a sample.yml file of:
a:
array:
- 1
- 2
- animal: dog
value: coconut
b:
array:
- 3
- 4
- animal: cat
value: banana
then
yq '.a *+ .b' sample.yml
will output
array:
- 1
- 2
- animal: dog
- 3
- 4
- animal: cat
value: banana
Given a sample.yml file of:
a:
thing:
- 1
- 2
b:
thing:
- 3
- 4
another:
- 1
then
yq '.a *?+ .b' sample.yml
will output
thing:
- 1
- 2
- 3
- 4
Merging arrays deeply means arrays are merged like objects, with indices 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: fred
age: 12
- name: bob
age: 32
b:
- name: fred
age: 34
then
yq '.a *d .b' sample.yml
will output
- name: fred
age: 34
- name: bob
age: 32
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.
Given a sample.yml file of:
myArray:
- a: apple
b: appleB
- a: kiwi
b: kiwiB
- a: banana
b: bananaB
something: else
And another sample another.yml file of:
newArray:
- a: banana
c: bananaC
- a: apple
b: appleB2
- a: dingo
c: dingoC
then
idPath=".a" originalPath=".myArray" otherPath=".newArray" yq eval-all '
(
(( (eval(strenv(originalPath)) + eval(strenv(otherPath))) | .[] | {(eval(strenv(idPath))): .}) as $item ireduce ({}; . * $item )) as $uniqueMap
| ( $uniqueMap | to_entries | .[]) as $item ireduce([]; . + $item.value)
) as $mergedArray
| select(fi == 0) | (eval(strenv(originalPath))) = $mergedArray
' sample.yml another.yml
will output
myArray:
- a: apple
b: appleB2
- a: kiwi
b: kiwiB
- a: banana
b: bananaB
c: bananaC
- a: dingo
c: dingoC
something: else
Given a sample.yml file of:
a: cat
b: dog
then
yq '. * {"a": {"c": .a}}' sample.yml
will output
a:
c: cat
b: dog
Given a sample.yml file of:
a: &cat
c: frog
b:
f: *cat
c:
g: thongs
then
yq '.c * .b' sample.yml
will output
g: thongs
f: *cat
Given a sample.yml file of:
a:
c: &cat frog
b:
f: *cat
c:
g: thongs
then
yq '.c * .a' sample.yml
will output
g: thongs
c: &cat frog
Given a sample.yml file of:
foo: &foo
a: foo_a
thing: foo_thing
c: foo_c
bar: &bar
b: bar_b
thing: bar_thing
c: bar_c
foobarList:
b: foobarList_b
!!merge <<:
- *foo
- *bar
c: foobarList_c
foobar:
c: foobar_c
!!merge <<: *foo
thing: foobar_thing
then
yq '.foobar * .foobarList' sample.yml
will output
c: foobarList_c
!!merge <<:
- *foo
- *bar
thing: foobar_thing
b: foobarList_b
When custom tags are encountered, yq will try to decode the underlying type.
Given a sample.yml file of:
a: !horse 2
b: !goat 3
then
yq '.a = .a * .b' sample.yml
will output
a: !horse 6
b: !goat 3
Custom tags will be maintained.
Given a sample.yml file of:
a: !horse
cat: meow
b: !goat
dog: woof
then
yq '.a = .a * .b' sample.yml
will output
a: !horse
cat: meow
dog: woof
b: !goat
dog: woof
Use the
c
option to clobber custom tags. Note that the second tag is now used.Given a sample.yml file of:
a: !horse
cat: meow
b: !goat
dog: woof
then
yq '.a *=c .b' sample.yml
will output
a: !goat
cat: meow
dog: woof
b: !goat
dog: woof
Running
yq --null-input 'null * {"some": "thing"}'
will output
some: thing
Running
yq --null-input '{"some": "thing"} * null'
will output
some: thing
Running
yq --null-input 'null * ["some"]'
will output
- some
Running
yq --null-input '["some"] * null'
will output
- some
Last modified 17d ago