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
  • to_entries Map
  • to_entries Array
  • to_entries null
  • from_entries map
  • from_entries with numeric key indices
  • Use with_entries to update keys
  • Use with_entries to update keys recursively
  • Custom sort map keys
  • Use with_entries to filter the map

Was this helpful?

  1. Operators

Entries

Similar to the same named functions in jq these functions convert to/from an object and an array of key-value pairs. This is most useful for performing operations on keys of maps.

Use with_entries(op) as a syntatic sugar for doing to_entries | op | from_entries.

to_entries Map

Given a sample.yml file of:

a: 1
b: 2

then

yq 'to_entries' sample.yml

will output

- key: a
  value: 1
- key: b
  value: 2

to_entries Array

Given a sample.yml file of:

- a
- b

then

yq 'to_entries' sample.yml

will output

- key: 0
  value: a
- key: 1
  value: b

to_entries null

Given a sample.yml file of:

null

then

yq 'to_entries' sample.yml

will output

from_entries map

Given a sample.yml file of:

a: 1
b: 2

then

yq 'to_entries | from_entries' sample.yml

will output

a: 1
b: 2

from_entries with numeric key indices

from_entries always creates a map, even for numeric keys

Given a sample.yml file of:

- a
- b

then

yq 'to_entries | from_entries' sample.yml

will output

0: a
1: b

Use with_entries to update keys

Given a sample.yml file of:

a: 1
b: 2

then

yq 'with_entries(.key |= "KEY_" + .)' sample.yml

will output

KEY_a: 1
KEY_b: 2

Use with_entries to update keys recursively

We use (.. | select(tag="map")) to find all the maps in the doc, then |= to update each one of those maps. In the update, with_entries is used.

Given a sample.yml file of:

a: 1
b:
  b_a: nested
  b_b: thing

then

yq '(.. | select(tag=="!!map")) |= with_entries(.key |= "KEY_" + .)' sample.yml

will output

KEY_a: 1
KEY_b:
  KEY_b_a: nested
  KEY_b_b: thing

Custom sort map keys

Use to_entries to convert to an array of key/value pairs, sort the array using sort/sort_by/etc, and convert it back.

Given a sample.yml file of:

a: 1
c: 3
b: 2

then

yq 'to_entries | sort_by(.key) | reverse | from_entries' sample.yml

will output

c: 3
b: 2
a: 1

Use with_entries to filter the map

Given a sample.yml file of:

a:
  b: bird
c:
  d: dog

then

yq 'with_entries(select(.value | has("b")))' sample.yml

will output

a:
  b: bird
PreviousEncode / DecodeNextEnv Variable Operators

Last updated 2 months ago

Was this helpful?