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

79 lines
1.0 KiB
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
2020-12-26 22:36:33 +00:00
'': null
2020-11-17 22:44:16 +00:00
```
then
```bash
yq eval '{"wrap": .}' sample.yml
```
will output
```yaml
2020-11-19 05:45:05 +00:00
wrap:
name: Mike
2020-12-26 22:36:33 +00:00
'': null
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
2020-12-26 22:36:33 +00:00
pets: [cat, dog]
'': null
2020-11-17 22:44:16 +00:00
```
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
2020-12-26 22:36:33 +00:00
pets: [cat, dog]
'': null
2020-11-17 22:44:16 +00:00
---
name: Rosey
2020-12-26 22:36:33 +00:00
pets: [monkey, sheep]
'': null
2020-11-17 22:44:16 +00:00
```
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
```