yq
v4.x
v4.x
  • yq
  • How It Works
  • Recipes
  • Upgrading from V3
  • Commands
    • Evaluate
    • Evaluate All
    • Shell Completion
  • Operators
    • Add
    • Alternative (Default value)
    • Anchor and Alias Operators
    • Array to Map
    • Assign (Update)
    • Boolean Operators
    • Collect into Array
    • Column
    • Comment Operators
    • Compare Operators
    • Contains
    • Create, Collect into Object
    • Date Time
    • Delete
    • Divide
    • Document Index
    • Encode / Decode
    • Entries
    • Env Variable Operators
    • Equals
    • Eval
    • File Operators
    • Filter Operator
    • Flatten
    • Group By
    • Has
    • Keys
    • Kind
    • Length
    • Line
    • Load
    • Min
    • Map
    • Max
    • Modulo
    • Multiply (Merge)
    • Omit
    • Parent
    • Path
    • Pick
    • Pipe
    • Pivot
    • Recursive Descent (Glob)
    • Reduce
    • Reverse
    • Select
    • Shuffle
    • Slice Array
    • Sort
    • Sort Keys
    • Split into Documents
    • String Operators
    • Style
    • Subtract
    • Tag
    • To Number
    • Traverse (Read)
    • Union
    • Unique
    • Variable Operators
    • With
  • Usage
    • Output format
    • Working with CSV, TSV
    • Working with JSON
    • Working with Properties
    • Working with XML
    • Working with LUA
    • Working with TOML
    • Working with Shell Output
    • Front Matter
    • Split into multiple files
    • GitHub Action
    • Tips, Tricks, Troubleshooting
  • Github Page
Powered by GitBook
On this page
  • Multiply (Merge)
  • Objects and arrays - merging
  • Merging complex arrays together by a key field
  • Multiply integers
  • Multiply string node X int
  • Multiply int X string node
  • Multiply string X int node
  • Multiply int node X string
  • Merge objects together, returning merged result only
  • Merge objects together, returning parent object
  • Merge keeps style of LHS
  • Merge arrays
  • Merge, only existing fields
  • Merge, only new fields
  • Merge, appending arrays
  • Merge, only existing fields, appending arrays
  • Merge, deeply merging arrays
  • Merge arrays of objects together, matching on a key
  • Merge to prefix an element
  • Merge with simple aliases
  • Merge copies anchor names
  • Merge with merge anchors
  • Custom types: that are really numbers
  • Custom types: that are really maps
  • Custom types: clobber tags
  • Merging a null with a map
  • Merging a map with null
  • Merging a null with an array
  • Merging an array with null

Was this helpful?

  1. Operators

Multiply (Merge)

PreviousModuloNextOmit

Last updated 1 year ago

Was this helpful?

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 .

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.

yq eval-all '. as $item ireduce ({}; . * $item )' *.yml

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.

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

then

yq '.a *n .b' sample.yml

will output

thing: one
cat: frog
missing: two

Merge, appending arrays

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

Merge, only existing fields, appending arrays

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

Merge, 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: 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.

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

Merge to prefix an element

Given a sample.yml file of:

a: cat
b: dog

then

yq '. * {"a": {"c": .a}}' sample.yml

will output

a:
  c: cat
b: dog

Merge with simple aliases

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

Merge copies anchor names

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

Merge 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_thing

then

yq '.foobar * .foobarList' sample.yml

will output

c: foobarList_c
!!merge <<:
  - *foo
  - *bar
thing: foobar_thing
b: foobarList_b

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:

a: !horse 2
b: !goat 3

then

yq '.a = .a * .b' sample.yml

will output

a: !horse 6
b: !goat 3

Custom types: that are really maps

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

Custom 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: woof

then

yq '.a *=c .b' sample.yml

will output

a: !goat
  cat: meow
  dog: woof
b: !goat
  dog: woof

Merging a null with a map

Running

yq --null-input 'null * {"some": "thing"}'

will output

some: thing

Merging a map with null

Running

yq --null-input '{"some": "thing"} * null'

will output

some: thing

Merging a null with an array

Running

yq --null-input 'null * ["some"]'

will output

- some

Merging an array with null

Running

yq --null-input '["some"] * null'

will output

- some

For more complex array merging (e.g. merging items that match on a certain key) please see the example

Thanks Kev from

here
here
here
stackoverflow