2021-11-03 02:54:09 +00:00
|
|
|
# Flatten
|
2021-10-26 04:42:25 +00:00
|
|
|
This recursively flattens arrays.
|
|
|
|
|
|
|
|
## Flatten
|
|
|
|
Recursively flattens all arrays
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 05:19:58 +00:00
|
|
|
[1, [2], [[3]]]
|
2021-10-26 04:42:25 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq 'flatten' sample.yml
|
2021-10-26 04:42:25 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
2023-05-05 05:19:58 +00:00
|
|
|
[1, 2, 3]
|
2021-10-26 04:42:25 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Flatten with depth of one
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 05:19:58 +00:00
|
|
|
[1, [2], [[3]]]
|
2021-10-26 04:42:25 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq 'flatten(1)' sample.yml
|
2021-10-26 04:42:25 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
2023-05-05 05:19:58 +00:00
|
|
|
[1, 2, [3]]
|
2021-10-26 04:42:25 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Flatten empty array
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 05:19:58 +00:00
|
|
|
[[]]
|
2021-10-26 04:42:25 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq 'flatten' sample.yml
|
2021-10-26 04:42:25 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
[]
|
|
|
|
```
|
|
|
|
|
|
|
|
## Flatten array of objects
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
2023-05-05 05:19:58 +00:00
|
|
|
[{foo: bar}, [{foo: baz}]]
|
2021-10-26 04:42:25 +00:00
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-27 06:21:10 +00:00
|
|
|
yq 'flatten' sample.yml
|
2021-10-26 04:42:25 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
2023-05-05 05:19:58 +00:00
|
|
|
[{foo: bar}, {foo: baz}]
|
2021-10-26 04:42:25 +00:00
|
|
|
```
|
|
|
|
|