y
y
yq
Search…
Create, Collect into Object
This is used to construct objects (or maps). This can be used against existing yaml, or to create fresh yaml documents.
Note that versions prior to 4.18 require the 'eval/e' command to be specified.
yq e <exp> <file>

Collect empty object

Running
1
yq --null-input '{}'
Copied!
will output
1
{}
Copied!

Wrap (prefix) existing object

Given a sample.yml file of:
1
name: Mike
Copied!
then
1
yq '{"wrap": .}' sample.yml
Copied!
will output
1
wrap:
2
name: Mike
Copied!

Using splat to create multiple objects

Given a sample.yml file of:
1
name: Mike
2
pets:
3
- cat
4
- dog
Copied!
then
1
yq '{.name: .pets.[]}' sample.yml
Copied!
will output
1
Mike: cat
2
Mike: dog
Copied!

Working with multiple documents

Given a sample.yml file of:
1
name: Mike
2
pets:
3
- cat
4
- dog
5
---
6
name: Rosey
7
pets:
8
- monkey
9
- sheep
Copied!
then
1
yq '{.name: .pets.[]}' sample.yml
Copied!
will output
1
Mike: cat
2
Mike: dog
3
Rosey: monkey
4
Rosey: sheep
Copied!

Creating yaml from scratch

Running
1
yq --null-input '{"wrap": "frog"}'
Copied!
will output
1
wrap: frog
Copied!

Creating yaml from scratch with multiple objects

Running
1
yq --null-input '(.a.b = "foo") | (.d.e = "bar")'
Copied!
will output
1
a:
2
b: foo
3
d:
4
e: bar
Copied!
Last modified 1mo ago