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.
Collect empty object
Running
yq --null-input '{}'will output
{}Wrap (prefix) existing object
Given a sample.yml file of:
name: Mikethen
yq '{"wrap": .}' sample.ymlwill output
wrap:
name: MikeUsing splat to create multiple objects
Given a sample.yml file of:
name: Mike
pets:
- cat
- dogthen
yq '{.name: .pets.[]}' sample.ymlwill output
Mike: cat
Mike: dogWorking with multiple documents
Given a sample.yml file of:
name: Mike
pets:
- cat
- dog
---
name: Rosey
pets:
- monkey
- sheepthen
yq '{.name: .pets.[]}' sample.ymlwill output
Mike: cat
Mike: dog
---
Rosey: monkey
Rosey: sheepCreating yaml from scratch
Running
yq --null-input '{"wrap": "frog"}'will output
wrap: frogCreating yaml from scratch with multiple objects
Running
yq --null-input '(.a.b = "foo") | (.d.e = "bar")'will output
a:
b: foo
d:
e: barLast updated
Was this helpful?