mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-01 01:41:39 +00:00
Add the `in` operator to check if a value is present in a mapping (as a key) or sequence (as an element). This mirrors jq's `in` operator which is defined as the inverse of `has`. Usage: . as $m | "key" | in($m) -- check map key existence . as $m | "value" | in($m) -- check array value membership .[] | select(.type | in(["a","b"])) -- filter by membership Fixes #2322
1.4 KiB
1.4 KiB
Check key exists in map using variable binding
Given a sample.yml file of:
a: 1
b: 2
c: 3
then
yq '. as $m | "a" | in($m)' sample.yml
will output
true
Check key does not exist in map
Given a sample.yml file of:
a: 1
b: 2
c: 3
then
yq '. as $m | "d" | in($m)' sample.yml
will output
false
Check value exists in array
Given a sample.yml file of:
- Tool
- Food
- Flower
then
yq '. as $m | "Food" | in($m)' sample.yml
will output
true
Check value does not exist in array
Given a sample.yml file of:
- Tool
- Food
- Flower
then
yq '. as $m | "Animal" | in($m)' sample.yml
will output
false
Check in with select on array elements
Filter items whose type is in the given list
Given a sample.yml file of:
- item: Pizza
type: Food
- item: Rose
type: Flower
- item: Hammer
type: Tool
then
yq '.[] | select(.type | in(["Tool", "Food"]))' sample.yml
will output
item: Pizza
type: Food
item: Hammer
type: Tool
In with variable binding - found
Given a sample.yml file of:
a: 1
b: 2
c: 3
then
yq '. as $m | "b" | in($m)' sample.yml
will output
true
In with variable binding - not found
Given a sample.yml file of:
a: 1
b: 2
c: 3
then
yq '. as $m | "z" | in($m)' sample.yml
will output
false