mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-15 07:38:14 +00:00
89 lines
1.1 KiB
Markdown
89 lines
1.1 KiB
Markdown
# 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.
|
|
|
|
{% hint style="warning" %}
|
|
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
|
|
|
|
`yq e <exp> <file>`
|
|
{% endhint %}
|
|
|
|
## Collect empty object
|
|
Running
|
|
```bash
|
|
yq --null-input '{}'
|
|
```
|
|
will output
|
|
```yaml
|
|
{}
|
|
```
|
|
|
|
## Wrap (prefix) existing object
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
name: Mike
|
|
```
|
|
then
|
|
```bash
|
|
yq '{"wrap": .}' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
wrap:
|
|
name: Mike
|
|
```
|
|
|
|
## Using splat to create multiple objects
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
name: Mike
|
|
pets:
|
|
- cat
|
|
- dog
|
|
```
|
|
then
|
|
```bash
|
|
yq '{.name: .pets.[]}' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
Mike: cat
|
|
Mike: dog
|
|
```
|
|
|
|
## Working with multiple documents
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
name: Mike
|
|
pets:
|
|
- cat
|
|
- dog
|
|
---
|
|
name: Rosey
|
|
pets:
|
|
- monkey
|
|
- sheep
|
|
```
|
|
then
|
|
```bash
|
|
yq '{.name: .pets.[]}' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
Mike: cat
|
|
Mike: dog
|
|
Rosey: monkey
|
|
Rosey: sheep
|
|
```
|
|
|
|
## Creating yaml from scratch
|
|
Running
|
|
```bash
|
|
yq --null-input '{"wrap": "frog"}'
|
|
```
|
|
will output
|
|
```yaml
|
|
wrap: frog
|
|
```
|
|
|