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 arraysd
deeply merge arrays?
only merge existing fieldsn
only merge new fieldsc
clobber 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.
Merging all files
Note the use of eval-all
to ensure all documents are loaded into memory.
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
Multiply integers
Given a sample.yml file of:
then
will output
Multiply string node X int
Given a sample.yml file of:
then
will output
Multiply int X string node
Given a sample.yml file of:
then
will output
Multiply string X int node
Given a sample.yml file of:
then
will output
Multiply int node X string
Given a sample.yml file of:
then
will output
Merge objects together, returning merged result only
Given a sample.yml file of:
then
will output
Merge objects together, returning parent object
Given a sample.yml file of:
then
will output
Merge keeps style of LHS
Given a sample.yml file of:
then
will output
Merge arrays
Given a sample.yml file of:
then
will output
Merge, only existing fields
Given a sample.yml file of:
then
will output
Merge, only new fields
Given a sample.yml file of:
then
will output
Merge, appending arrays
Given a sample.yml file of:
then
will output
Merge, only existing fields, appending arrays
Given a sample.yml file of:
then
will output
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:
then
will output
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.
Thanks Kev from stackoverflow
Given a sample.yml file of:
And another sample another.yml file of:
then
will output
Merge to prefix an element
Given a sample.yml file of:
then
will output
Merge with simple aliases
Given a sample.yml file of:
then
will output
Merge copies anchor names
Given a sample.yml file of:
then
will output
Merge with merge anchors
Given a sample.yml file of:
then
will output
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:
then
will output
Custom types: that are really maps
Custom tags will be maintained.
Given a sample.yml file of:
then
will output
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:
then
will output
Merging a null with a map
Running
will output
Merging a map with null
Running
will output
Merging a null with an array
Running
will output
Merging an array with null
Running
will output
Last updated