yq/pkg/yqlib/doc/Create, Collect into Object.md

80 lines
1017 B
Markdown
Raw Normal View History

2020-11-17 22:44:16 +00:00
This is used to construct objects (or maps). This can be used against existing yaml, or to create fresh yaml documents.
2020-11-22 02:16:54 +00:00
## Collect empty object
2020-11-17 22:44:16 +00:00
Running
```bash
yq eval --null-input '{}'
```
will output
```yaml
2020-11-22 02:16:54 +00:00
{}
2020-11-17 22:44:16 +00:00
```
2020-11-22 02:16:54 +00:00
## Wrap (prefix) existing object
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
name: Mike
```
then
```bash
yq eval '{"wrap": .}' sample.yml
```
will output
```yaml
2020-11-19 05:45:05 +00:00
wrap:
name: Mike
2020-11-17 22:44:16 +00:00
```
2020-11-22 02:16:54 +00:00
## Using splat to create multiple objects
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
name: Mike
pets:
- cat
- dog
```
then
```bash
2020-11-22 02:50:32 +00:00
yq eval '{.name: .pets.[]}' sample.yml
2020-11-17 22:44:16 +00:00
```
will output
```yaml
Mike: cat
Mike: dog
```
2020-11-22 02:16:54 +00:00
## Working with multiple documents
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
name: Mike
pets:
- cat
- dog
---
name: Rosey
pets:
- monkey
- sheep
```
then
```bash
2020-11-22 02:50:32 +00:00
yq eval '{.name: .pets.[]}' sample.yml
2020-11-17 22:44:16 +00:00
```
will output
```yaml
Mike: cat
Mike: dog
Rosey: monkey
Rosey: sheep
```
2020-11-22 02:16:54 +00:00
## Creating yaml from scratch
2020-11-17 22:44:16 +00:00
Running
```bash
yq eval --null-input '{"wrap": "frog"}'
```
will output
```yaml
wrap: frog
```