2021-10-30 03:14:39 +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.
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-11-03 04:00:28 +00:00
## to_entries Map
2021-10-30 03:14:39 +00:00
Given a sample.yml file of:
```yaml
a: 1
b: 2
```
then
```bash
2022-01-28 01:45:43 +00:00
yq 'to_entries' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
- key: a
value: 1
- key: b
value: 2
```
2021-11-03 04:00:28 +00:00
## to_entries Array
2021-10-30 03:14:39 +00:00
Given a sample.yml file of:
```yaml
- a
- b
```
then
```bash
2022-01-28 01:45:43 +00:00
yq 'to_entries' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
- key: 0
value: a
- key: 1
value: b
```
2021-11-03 04:00:28 +00:00
## to_entries null
2021-10-30 03:14:39 +00:00
Given a sample.yml file of:
```yaml
null
```
then
```bash
2022-01-28 01:45:43 +00:00
yq 'to_entries' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
```
2021-11-03 04:00:28 +00:00
## from_entries map
2021-10-30 03:14:39 +00:00
Given a sample.yml file of:
```yaml
a: 1
b: 2
```
then
```bash
2022-01-28 01:45:43 +00:00
yq 'to_entries | from_entries' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a: 1
b: 2
```
2021-11-03 04:00:28 +00:00
## from_entries with numeric key indexes
from_entries always creates a map, even for numeric keys
2021-10-30 03:14:39 +00:00
Given a sample.yml file of:
```yaml
- a
- b
```
then
```bash
2022-01-28 01:45:43 +00:00
yq 'to_entries | from_entries' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
0: a
1: b
```
2021-11-03 04:00:28 +00:00
## Use with_entries to update keys
2021-10-30 03:14:39 +00:00
Given a sample.yml file of:
```yaml
a: 1
b: 2
```
then
```bash
2022-01-28 01:45:43 +00:00
yq 'with_entries(.key |= "KEY_" + .)' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
KEY_a: 1
KEY_b: 2
```
2021-11-03 04:00:28 +00:00
## Use with_entries to filter the map
2021-10-30 03:14:39 +00:00
Given a sample.yml file of:
```yaml
a:
b: bird
c:
d: dog
```
then
```bash
2022-01-28 01:45:43 +00:00
yq 'with_entries(select(.value | has("b")))' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a:
b: bird
```
2021-11-03 04:00:28 +00:00