2021-10-30 03:14:39 +00:00
# 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
2021-11-03 04:00:28 +00:00
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
2021-10-30 03:14:39 +00:00
2022-01-22 00:49:12 +00:00
- `+` append arrays
- `d` deeply merge arrays
- `?` only merge _existing_ fields
- `n` only merge _new_ fields
2022-08-29 08:31:50 +00:00
- `c` clobber custom tags
2021-10-30 03:14:39 +00:00
2022-03-20 02:05:09 +00:00
### Merge two files together
This uses the load operator to merge file2 into file1.
```bash
yq '. *= load("file2.yml")' file1.yml
```
### Merging all files
2021-10-30 03:14:39 +00:00
Note the use of `eval-all` to ensure all documents are loaded into memory.
```bash
2022-03-20 02:05:09 +00:00
yq eval-all '. as $item ireduce ({}; . * $item )' * .yml
2021-10-30 03:14:39 +00:00
```
2022-05-25 01:01:35 +00:00
# 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 ](https://mikefarah.gitbook.io/yq/operators/multiply-merge#merge-arrays-of-objects-together-matching-on-a-key )
2021-10-30 03:14:39 +00:00
## Multiply integers
2022-01-28 01:45:43 +00:00
Given a sample.yml file of:
```yaml
a: 3
b: 4
```
then
2021-10-30 03:14:39 +00:00
```bash
2022-01-28 01:45:43 +00:00
yq '.a *= .b' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
2022-01-28 01:45:43 +00:00
a: 12
b: 4
2021-10-30 03:14:39 +00:00
```
## Merge objects together, returning merged result only
Given a sample.yml file of:
```yaml
a:
field: me
fieldA: cat
b:
field:
g: wizz
fieldB: dog
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.a * .b' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
field:
g: wizz
fieldA: cat
fieldB: dog
```
## Merge objects together, returning parent object
Given a sample.yml file of:
```yaml
a:
field: me
fieldA: cat
b:
field:
g: wizz
fieldB: dog
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '. * {"a":.b}' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
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:
```yaml
a: {things: great}
b:
also: "me"
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '. * {"a":.b}' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a: {things: great, also: "me"}
b:
also: "me"
```
## Merge arrays
Given a sample.yml file of:
```yaml
a:
- 1
- 2
- 3
b:
- 3
- 4
- 5
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '. * {"a":.b}' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a:
- 3
- 4
- 5
b:
- 3
- 4
- 5
```
## Merge, only existing fields
Given a sample.yml file of:
```yaml
a:
thing: one
cat: frog
b:
missing: two
thing: two
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.a *? .b' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
thing: two
cat: frog
```
2022-01-22 00:49:12 +00:00
## Merge, only new fields
Given a sample.yml file of:
```yaml
a:
thing: one
cat: frog
b:
missing: two
thing: two
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.a *n .b' sample.yml
2022-01-22 00:49:12 +00:00
```
will output
```yaml
thing: one
cat: frog
missing: two
```
2021-10-30 03:14:39 +00:00
## Merge, appending arrays
Given a sample.yml file of:
```yaml
a:
array:
- 1
- 2
- animal: dog
value: coconut
b:
array:
- 3
- 4
- animal: cat
value: banana
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.a *+ .b' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
array:
- 1
- 2
- animal: dog
- 3
- 4
- animal: cat
value: banana
```
## Merge, only existing fields, appending arrays
Given a sample.yml file of:
```yaml
a:
thing:
- 1
- 2
b:
thing:
- 3
- 4
another:
- 1
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.a *?+ .b' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
thing:
- 1
- 2
- 3
- 4
```
## Merge, deeply merging arrays
2023-03-18 03:54:23 +00:00
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.
2021-10-30 03:14:39 +00:00
Given a sample.yml file of:
```yaml
a:
- name: fred
age: 12
- name: bob
age: 32
b:
- name: fred
age: 34
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.a *d .b' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
- name: fred
age: 34
- name: bob
age: 32
```
## Merge arrays of objects together, matching on a key
2022-02-06 03:41:27 +00:00
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:
2021-12-05 02:03:45 +00:00
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.
2022-02-06 03:41:27 +00:00
First the expression will create a map from the arrays keyed by the idPath, the unique field we want to merge by.
2021-12-05 02:03:45 +00:00
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.
2021-11-29 10:29:05 +00:00
Finally, we set the result of the merged array back into the first doc.
2021-11-29 09:56:49 +00:00
Thanks Kev from [stackoverflow ](https://stackoverflow.com/a/70109529/1168223 )
2021-10-30 03:14:39 +00:00
Given a sample.yml file of:
```yaml
2021-11-29 10:29:05 +00:00
myArray:
- a: apple
b: appleB
- a: kiwi
b: kiwiB
- a: banana
b: bananaB
something: else
2021-10-30 03:14:39 +00:00
```
And another sample another.yml file of:
```yaml
2022-02-06 03:41:27 +00:00
newArray:
2021-11-29 10:29:05 +00:00
- a: banana
c: bananaC
- a: apple
b: appleB2
- a: dingo
c: dingoC
2021-10-30 03:14:39 +00:00
```
then
```bash
2022-02-06 03:41:27 +00:00
idPath=".a" originalPath=".myArray" otherPath=".newArray" yq eval-all '
2021-11-29 10:29:05 +00:00
(
2022-02-06 03:41:27 +00:00
(( (eval(strenv(originalPath)) + eval(strenv(otherPath))) | .[] | {(eval(strenv(idPath))): .}) as $item ireduce ({}; . * $item )) as $uniqueMap
2021-11-29 10:06:00 +00:00
| ( $uniqueMap | to_entries | .[]) as $item ireduce([]; . + $item.value)
2021-11-29 10:29:05 +00:00
) as $mergedArray
2022-02-06 03:41:27 +00:00
| select(fi == 0) | (eval(strenv(originalPath))) = $mergedArray
2021-11-29 09:56:49 +00:00
' sample.yml another.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
2021-11-29 10:29:05 +00:00
myArray:
- a: apple
b: appleB2
- a: kiwi
b: kiwiB
- a: banana
b: bananaB
c: bananaC
- a: dingo
c: dingoC
something: else
2021-10-30 03:14:39 +00:00
```
## Merge to prefix an element
Given a sample.yml file of:
```yaml
a: cat
b: dog
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '. * {"a": {"c": .a}}' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a:
c: cat
b: dog
```
## Merge with simple aliases
Given a sample.yml file of:
```yaml
a: & cat
c: frog
b:
f: *cat
c:
g: thongs
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.c * .b' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
g: thongs
f: *cat
```
## Merge copies anchor names
Given a sample.yml file of:
```yaml
a:
c: & cat frog
b:
f: *cat
c:
g: thongs
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.c * .a' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
g: thongs
c: & cat frog
```
## Merge with merge anchors
Given a sample.yml file of:
```yaml
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
```bash
2022-01-28 01:45:43 +00:00
yq '.foobar * .foobarList' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
c: foobarList_c
2021-11-03 04:00:28 +00:00
!!merge < < :
2021-10-30 03:14:39 +00:00
- *foo
- *bar
thing: foobar_thing
b: foobarList_b
```
2021-11-03 04:00:28 +00:00
2022-01-28 01:45:43 +00:00
## 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:
```yaml
a: !horse 2
b: !goat 3
```
then
```bash
yq '.a = .a * .b' sample.yml
```
will output
```yaml
a: !horse 6
b: !goat 3
```
## Custom types: that are really maps
Custom tags will be maintained.
Given a sample.yml file of:
```yaml
a: !horse
cat: meow
b: !goat
dog: woof
```
then
```bash
yq '.a = .a * .b' sample.yml
```
will output
```yaml
a: !horse
cat: meow
dog: woof
b: !goat
dog: woof
```
2022-08-29 08:31:50 +00:00
## Custom types: clobber tags
2023-03-18 03:54:23 +00:00
Use the `c` option to clobber custom tags. Note that the second tag is now used.
2022-08-29 08:31:50 +00:00
Given a sample.yml file of:
```yaml
a: !horse
cat: meow
b: !goat
dog: woof
```
then
```bash
yq '.a *=c .b' sample.yml
```
will output
```yaml
a: !goat
cat: meow
dog: woof
b: !goat
dog: woof
```
2023-01-14 03:06:09 +00:00
## Merging a null with a map
Running
```bash
yq --null-input 'null * {"some": "thing"}'
```
will output
```yaml
some: thing
```
## Merging a map with null
Running
```bash
yq --null-input '{"some": "thing"} * null'
```
will output
```yaml
some: thing
```
2023-11-23 00:59:14 +00:00
## Merging a null with an array
2023-01-14 03:06:09 +00:00
Running
```bash
yq --null-input 'null * ["some"]'
```
will output
```yaml
- some
```
## Merging an array with null
Running
```bash
yq --null-input '["some"] * null'
```
will output
```yaml
- some
```