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.
You can use the add operator +, to shallow merge objects, see more info here.
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 arraysddeeply merge arrays?only merge existing fieldsnonly merge new fieldscclobber custom tags
To perform a shallow merge only, use the add operator +, see more info here.
Merge two files together
This uses the load operator to merge file2 into file1.
yq '. *= load("file2.yml")' file1.ymlMerging all files
Note the use of eval-all to ensure all documents are loaded into memory.
yq eval-all '. as $item ireduce ({}; . * $item )' *.ymlMerging 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
Multiply integers
Given a sample.yml file of:
a: 3
b: 4then
yq '.a *= .b' sample.ymlwill output
a: 12
b: 4Multiply string node X int
Given a sample.yml file of:
b: bananathen
yq '.b * 4' sample.ymlwill output
bananabananabananabananaMultiply int X string node
Given a sample.yml file of:
b: bananathen
yq '4 * .b' sample.ymlwill output
bananabananabananabananaMultiply string X int node
Given a sample.yml file of:
n: 4then
yq '"banana" * .n' sample.ymlwill output
bananabananabananabananaMultiply int node X string
Given a sample.yml file of:
n: 4then
yq '.n * "banana"' sample.ymlwill output
bananabananabananabananaMerge objects together, returning merged result only
Given a sample.yml file of:
a:
field: me
fieldA: cat
b:
field:
g: wizz
fieldB: dogthen
yq '.a * .b' sample.ymlwill output
field:
g: wizz
fieldA: cat
fieldB: dogMerge objects together, returning parent object
Given a sample.yml file of:
a:
field: me
fieldA: cat
b:
field:
g: wizz
fieldB: dogthen
yq '. * {"a":.b}' sample.ymlwill output
a:
field:
g: wizz
fieldA: cat
fieldB: dog
b:
field:
g: wizz
fieldB: dogMerge keeps style of LHS
Given a sample.yml file of:
a: {things: great}
b:
also: "me"then
yq '. * {"a":.b}' sample.ymlwill output
a: {things: great, also: "me"}
b:
also: "me"Merge arrays
Given a sample.yml file of:
a:
- 1
- 2
- 3
b:
- 3
- 4
- 5then
yq '. * {"a":.b}' sample.ymlwill output
a:
- 3
- 4
- 5
b:
- 3
- 4
- 5Merge, only existing fields
Given a sample.yml file of:
a:
thing: one
cat: frog
b:
missing: two
thing: twothen
yq '.a *? .b' sample.ymlwill output
thing: two
cat: frogMerge, only new fields
Given a sample.yml file of:
a:
thing: one
cat: frog
b:
missing: two
thing: twothen
yq '.a *n .b' sample.ymlwill output
thing: one
cat: frog
missing: twoMerge, appending arrays
Given a sample.yml file of:
a:
array:
- 1
- 2
- animal: dog
value: coconut
b:
array:
- 3
- 4
- animal: cat
value: bananathen
yq '.a *+ .b' sample.ymlwill output
array:
- 1
- 2
- animal: dog
- 3
- 4
- animal: cat
value: bananaMerge, only existing fields, appending arrays
Given a sample.yml file of:
a:
thing:
- 1
- 2
b:
thing:
- 3
- 4
another:
- 1then
yq '.a *?+ .b' sample.ymlwill output
thing:
- 1
- 2
- 3
- 4Merge, deeply merging arrays
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: 34then
yq '.a *d .b' sample.ymlwill output
- name: fred
age: 34
- name: bob
age: 32Merge 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:
myArray:
- a: apple
b: appleB
- a: kiwi
b: kiwiB
- a: banana
b: bananaB
something: elseAnd another sample another.yml file of:
newArray:
- a: banana
c: bananaC
- a: apple
b: appleB2
- a: dingo
c: dingoCthen
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.ymlwill output
myArray:
- a: apple
b: appleB2
- a: kiwi
b: kiwiB
- a: banana
b: bananaB
c: bananaC
- a: dingo
c: dingoC
something: elseMerge to prefix an element
Given a sample.yml file of:
a: cat
b: dogthen
yq '. * {"a": {"c": .a}}' sample.ymlwill output
a:
c: cat
b: dogMerge with simple aliases
Given a sample.yml file of:
a: &cat
c: frog
b:
f: *cat
c:
g: thongsthen
yq '.c * .b' sample.ymlwill output
g: thongs
f: *catMerge copies anchor names
Given a sample.yml file of:
a:
c: &cat frog
b:
f: *cat
c:
g: thongsthen
yq '.c * .a' sample.ymlwill output
g: thongs
c: &cat frogMerge with merge anchors
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_thingthen
yq '.foobar * .foobarList' sample.ymlwill output
c: foobarList_c
!!merge <<:
- *foo
- *bar
thing: foobar_thing
b: foobarList_bCustom types: that are really numbers
When custom tags are encountered, yq will try to decode the underlying type.
Given a sample.yml file of:
a: !horse 2
b: !goat 3then
yq '.a = .a * .b' sample.ymlwill output
a: !horse 6
b: !goat 3Custom types: that are really maps
Custom tags will be maintained.
Given a sample.yml file of:
a: !horse
cat: meow
b: !goat
dog: woofthen
yq '.a = .a * .b' sample.ymlwill output
a: !horse
cat: meow
dog: woof
b: !goat
dog: woofCustom types: clobber tags
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: woofthen
yq '.a *=c .b' sample.ymlwill output
a: !goat
cat: meow
dog: woof
b: !goat
dog: woofMerging a null with a map
Running
yq --null-input 'null * {"some": "thing"}'will output
some: thingMerging a map with null
Running
yq --null-input '{"some": "thing"} * null'will output
some: thingMerging a null with an array
Running
yq --null-input 'null * ["some"]'will output
- someMerging an array with null
Running
yq --null-input '["some"] * null'will output
- someLast updated
Was this helpful?