2021-10-30 03:14:39 +00:00
|
|
|
# Has
|
|
|
|
|
|
|
|
This is operation that returns true if the key exists in a map (or index in an array), false otherwise.
|
|
|
|
|
|
|
|
## Has map key
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- a: yes
|
|
|
|
- a: ~
|
|
|
|
- a:
|
|
|
|
- b: nope
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq '.[] | has("a")' sample.yml
|
2021-10-30 03:14:39 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
true
|
|
|
|
true
|
|
|
|
true
|
|
|
|
false
|
|
|
|
```
|
|
|
|
|
|
|
|
## Select, checking for existence of deep paths
|
|
|
|
Simply pipe in parent expressions into `has`
|
|
|
|
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- a:
|
|
|
|
b:
|
|
|
|
c: cat
|
|
|
|
- a:
|
|
|
|
b:
|
|
|
|
d: dog
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq '.[] | select(.a.b | has("c"))' sample.yml
|
2021-10-30 03:14:39 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a:
|
|
|
|
b:
|
|
|
|
c: cat
|
|
|
|
```
|
|
|
|
|
|
|
|
## Has array index
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
- []
|
|
|
|
- [1]
|
|
|
|
- [1, 2]
|
|
|
|
- [1, null]
|
|
|
|
- [1, 2, 3]
|
|
|
|
|
2021-11-03 04:00:28 +00:00
|
|
|
```
|
2021-10-30 03:14:39 +00:00
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq '.[] | has(1)' sample.yml
|
2021-10-30 03:14:39 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
false
|
|
|
|
false
|
|
|
|
true
|
|
|
|
true
|
|
|
|
true
|
|
|
|
```
|
2021-11-03 04:00:28 +00:00
|
|
|
|