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
  • Map keys
  • Array keys
  • Retrieve array key
  • Retrieve map key
  • No key
  • Update map key
  • Get comment from map key
  • Check node is a key

Was this helpful?

  1. Operators

Keys

Use the keys operator to return map keys or array indices.

Map keys

Given a sample.yml file of:

dog: woof
cat: meow

then

yq 'keys' sample.yml

will output

- dog
- cat

Array keys

Given a sample.yml file of:

- apple
- banana

then

yq 'keys' sample.yml

will output

- 0
- 1

Retrieve array key

Given a sample.yml file of:

- 1
- 2
- 3

then

yq '.[1] | key' sample.yml

will output

1

Retrieve map key

Given a sample.yml file of:

a: thing

then

yq '.a | key' sample.yml

will output

a

No key

Given a sample.yml file of:

{}

then

yq 'key' sample.yml

will output

Update map key

Given a sample.yml file of:

a:
  x: 3
  y: 4

then

yq '(.a.x | key) = "meow"' sample.yml

will output

a:
  meow: 3
  y: 4

Get comment from map key

Given a sample.yml file of:

a:
  # comment on key
  x: 3
  y: 4

then

yq '.a.x | key | headComment' sample.yml

will output

comment on key

Check node is a key

Given a sample.yml file of:

a:
  b:
    - cat
  c: frog

then

yq '[... | { "p": path | join("."), "isKey": is_key, "tag": tag }]' sample.yml

will output

- p: ""
  isKey: false
  tag: '!!map'
- p: a
  isKey: true
  tag: '!!str'
- p: a
  isKey: false
  tag: '!!map'
- p: a.b
  isKey: true
  tag: '!!str'
- p: a.b
  isKey: false
  tag: '!!seq'
- p: a.b.0
  isKey: false
  tag: '!!str'
- p: a.c
  isKey: true
  tag: '!!str'
- p: a.c
  isKey: false
  tag: '!!str'
PreviousHasNextKind

Last updated 2 years ago

Was this helpful?