2021-11-03 04:00:58 +00:00
# Entries
Similar to the same named functions in `jq` these functions convert to/from an object and an array of key-value pairs. This is most useful for performing operations on keys of maps.
## to_entries Map
Given a sample.yml file of:
```yaml
2023-05-02 05:07:04 +00:00
{a: 1, b: 2}
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'to_entries' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
- key: a
value: 1
- key: b
value: 2
```
## to_entries Array
Given a sample.yml file of:
```yaml
2023-05-02 05:07:04 +00:00
[a, b]
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'to_entries' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
- key: 0
value: a
- key: 1
value: b
```
## to_entries null
Given a sample.yml file of:
```yaml
2023-05-02 05:07:04 +00:00
[]
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'to_entries' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
2023-05-02 05:07:04 +00:00
[]
2021-11-03 04:00:58 +00:00
```
## from_entries map
Given a sample.yml file of:
```yaml
2023-05-02 05:07:04 +00:00
{a: 1, b: 2}
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'to_entries | from_entries' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
2023-05-04 09:46:35 +00:00
a: 1
b: 2
2021-11-03 04:00:58 +00:00
```
2023-03-16 02:39:36 +00:00
## from_entries with numeric key indices
2021-11-03 04:00:58 +00:00
from_entries always creates a map, even for numeric keys
Given a sample.yml file of:
```yaml
2023-05-02 05:07:04 +00:00
[a, b]
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'to_entries | from_entries' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
2023-05-04 09:46:35 +00:00
0: a
1: b
2021-11-03 04:00:58 +00:00
```
## Use with_entries to update keys
Given a sample.yml file of:
```yaml
2023-05-02 05:07:04 +00:00
{a: 1, b: 2}
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'with_entries(.key |= "KEY_" + .)' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
2023-05-04 09:46:35 +00:00
KEY_a: 1
KEY_b: 2
2021-11-03 04:00:58 +00:00
```
2022-10-17 04:03:47 +00:00
## Custom sort map keys
Use to_entries to convert to an array of key/value pairs, sort the array using sort/sort_by/etc, and convert it back.
Given a sample.yml file of:
```yaml
2023-05-02 05:07:04 +00:00
{a: 1, c: 3, b: 2}
2022-10-17 04:03:47 +00:00
```
then
```bash
yq 'to_entries | sort_by(.key) | reverse | from_entries' sample.yml
```
will output
```yaml
2023-05-04 09:46:35 +00:00
c: 3
b: 2
a: 1
2022-10-17 04:03:47 +00:00
```
2021-11-03 04:00:58 +00:00
## Use with_entries to filter the map
Given a sample.yml file of:
```yaml
2023-05-02 05:07:04 +00:00
{a: {b: bird}, c: {d: dog}}
2021-11-03 04:00:58 +00:00
```
then
```bash
2022-01-27 06:21:10 +00:00
yq 'with_entries(select(.value | has("b")))' sample.yml
2021-11-03 04:00:58 +00:00
```
will output
```yaml
2023-05-04 09:46:35 +00:00
a: {b: bird}
2021-11-03 04:00:58 +00:00
```