mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-03 19:05:38 +00:00
Add informational hints to operator documentation covering behaviors
that may differ from user expectations, particularly for those
familiar with jq:
- slice-array: clarify that slicing only works on arrays, not strings
- select: document behavior when piping select to a literal value
- equals: note bash history expansion with != operator
- create-collect-into-object: mention that shorthand {key} syntax
is not supported
- collect-into-array: explain difference between [.x | .[]] and [.x[]]
103 lines
1.4 KiB
Markdown
103 lines
1.4 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="info" %}
|
|
|
|
_Note_ for jq users: yq does not support shorthand object construction like `{name, version}`. Use the explicit form `{"name": .name, "version": .version}` or `pick(["name", "version"])` instead.
|
|
|
|
{% 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
|
|
```
|
|
|
|
## Creating yaml from scratch with multiple objects
|
|
Running
|
|
```bash
|
|
yq --null-input '(.a.b = "foo") | (.d.e = "bar")'
|
|
```
|
|
will output
|
|
```yaml
|
|
a:
|
|
b: foo
|
|
d:
|
|
e: bar
|
|
```
|
|
|