2021-10-30 04:52:05 +00:00
|
|
|
# Flatten
|
|
|
|
This recursively flattens arrays.
|
|
|
|
|
2022-02-06 03:41:27 +00:00
|
|
|
{% hint style="warning" %}
|
|
|
|
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
|
|
|
|
|
|
|
|
`yq e <exp> <file>`
|
|
|
|
{% endhint %}
|
|
|
|
|
2021-10-30 04:52:05 +00:00
|
|
|
## Flatten
|
|
|
|
Recursively flattens all arrays
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- 1
|
|
|
|
- - 2
|
|
|
|
- - - 3
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq 'flatten' sample.yml
|
2021-10-30 04:52:05 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
- 3
|
|
|
|
```
|
|
|
|
|
|
|
|
## Flatten with depth of one
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- 1
|
|
|
|
- - 2
|
|
|
|
- - - 3
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq 'flatten(1)' sample.yml
|
2021-10-30 04:52:05 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
- 1
|
|
|
|
- 2
|
|
|
|
- - 3
|
|
|
|
```
|
|
|
|
|
|
|
|
## Flatten empty array
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- []
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq 'flatten' sample.yml
|
2021-10-30 04:52:05 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
[]
|
|
|
|
```
|
|
|
|
|
|
|
|
## Flatten array of objects
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- foo: bar
|
|
|
|
- - foo: baz
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq 'flatten' sample.yml
|
2021-10-30 04:52:05 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
- foo: bar
|
|
|
|
- foo: baz
|
|
|
|
```
|
|
|
|
|