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 .
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
c clobber custom tags
To perform a shallow merge only, use the add operator +, see more info .
Merge two files together
This uses the load operator to merge file2 into file1.
yq '. *= load("file2.yml")' file1.yml
Merging all files
Note the use of eval-all to ensure all documents are loaded into memory.
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.
Multiply integers
Given a sample.yml file of:
a: 3
b: 4
then
yq '.a *= .b' sample.yml
will output
a: 12
b: 4
Multiply string node X int
Given a sample.yml file of:
b: banana
then
yq '.b * 4' sample.yml
will output
bananabananabananabanana
Multiply int X string node
Given a sample.yml file of:
b: banana
then
yq '4 * .b' sample.yml
will output
bananabananabananabanana
Multiply string X int node
Given a sample.yml file of:
n: 4
then
yq '"banana" * .n' sample.yml
will output
bananabananabananabanana
Multiply int node X string
Given a sample.yml file of:
n: 4
then
yq '.n * "banana"' sample.yml
will output
bananabananabananabanana
Merge objects together, returning merged result only
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
Merge objects together, returning parent object
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
Merge keeps style of LHS
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"
Merge arrays
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
Merge, only existing fields
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
Merge, only new fields
Given a sample.yml file of:
a:
thing: one
cat: frog
b:
missing: two
thing: two
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
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.