You don't need to use --yaml-output flag, which tries to parse the output of the filter defined as a YAML entity. For getting the raw strings, use a filter in the JSON context itself
yq -r '.images.tags[]'
Answer from Inian on Stack OverflowYou don't need to use --yaml-output flag, which tries to parse the output of the filter defined as a YAML entity. For getting the raw strings, use a filter in the JSON context itself
yq -r '.images.tags[]'
Found the answer, Inian kind of posted an answer but there was a small change
yq -r '.images.tags | to_entries[].value | split(":")[0]' values.yaml
shell - Yq: retrieve object keys names - Stack Overflow
bash - How to use `yq` to select key-value pairs and format them into "$key=$value" style outputs? - Stack Overflow
Selecting elements from map could be simplified
How do I select multiple keys for output?
keys is a built-in function in jq when given an object, returns its keys in an array. So it is not actually apart of your yaml (not a property) which means you cannot do services.keys.
To get the keys you can do the following when using Python yq:
We will get the object of services in the first part then we pass it to keys which will return a list of keys based on a given object
cat docker-compose.yml | yq '.services | keys'
Or like this (without cat and pipe):
yq '.services | keys' docker-compose.yml
The output will be:
[
"apache",
"mysql",
"php"
]
To get rid of the brackets:
yq '.services | keys[]' docker-compose.yml
The output:
"apache"
"mysql"
"php"
For more about details you can check Builtin operators and functions in jq. Note that yq is a wrapper for jq so the documentation of jq would be helpful as the help of yq recommends.
On Go yq you can do
yq e '.services | keys'
Since you just want to list the services from a docker-compose file you could achieve this with a docker-compose command.
docker-compose config --services
Not directly an answer to the question as it is not using yq but maybe it helps ;)
You could switch to kislyuk's yq which uses native jq under the hood. Then, you would just need to_entries to access key and value, string interpolation in combination with the -r flag to produce the output, and @sh to escape for shell compliance:
yq -r 'to_entries[] | "export \(.key)=\(.value | @sh)"'
export FOO='somefoo'
export BAR='somebar'
The @sh operator has been added in yq v4.31.1 (with my humble contribution). Now you can do it pretty much the same way as in jq:
yq '.[] | "export " + key + "=" + @sh'
The quoting algorithm is a bit different from jq as it starts to quote only at characters that need quoting, so the literal output will likely differ, but will be later parsed equally.
# input
FOO: somefoo
BAR: somebar and some
# result
export FOO=somefoo
export BAR=somebar' and some'
With older yq versions you can still implement a primitive but safe quoting algorithm using other yq functions (the quoting is a little lovely nightmare, though):
# POSIX shell quoting starting "
yq ".[] | \"export \" + key + \"='\" + sub(\"'\", \"'\''\") + \"'\""
# POSIX shell quoting starting '
yq '.[] | "export " + key + "='\''" + sub("'\''", "'\'\\\'\''") + "'\''"'
# BASH "dollar" quoting
yq $'.[] | "export " + key + "=\'" + sub("\'", "\'\\\'\'") + "\'"'
Actually this is exactly what jq does with its @sh. In all cases this ends up as:
export FOO='somefoo'
export BAR='somebar and some'