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
  • yq vs jq syntax
  • Sum numbers
  • Merge all yaml files together
  • Convert an array to an object

Was this helpful?

  1. Operators

Reduce

Reduce is a powerful way to process a collection of data into a new form.

<exp> as $<name> ireduce (<init>; <block>)

e.g.

.[] as $item ireduce (0; . + $item)

On the LHS we are configuring the collection of items that will be reduced <exp> as well as what each element will be called $<name>. Note that the array has been splatted into its individual elements.

On the RHS there is <init>, the starting value of the accumulator and <block>, the expression that will update the accumulator for each element in the collection. Note that within the block expression, . will evaluate to the current value of the accumulator.

yq vs jq syntax

Reduce syntax in yq is a little different from jq - as yq (currently) isn't as sophisticated as jq and its only supports infix notation (e.g. a + b, where the operator is in the middle of the two parameters) - where as jq uses a mix of infix notation with prefix notation (e.g. reduce a b is like writing + a b).

To that end, the reduce operator is called ireduce for backwards compatibility if a jq like prefix version of reduce is ever added.

Sum numbers

Given a sample.yml file of:

- 10
- 2
- 5
- 3

then

yq '.[] as $item ireduce (0; . + $item)' sample.yml

will output

20

Merge all yaml files together

Given a sample.yml file of:

a: cat

And another sample another.yml file of:

b: dog

then

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

will output

a: cat
b: dog

Convert an array to an object

Given a sample.yml file of:

- name: Cathy
  has: apples
- name: Bob
  has: bananas

then

yq '.[] as $item ireduce ({}; .[$item | .name] = ($item | .has) )' sample.yml

will output

Cathy: apples
Bob: bananas
PreviousRecursive Descent (Glob)NextReverse

Last updated 2 years ago

Was this helpful?