-
Hi all, I'm trying to use Dasel to select (filter) one
which returns a json with:
I first tried to only use a select:
Using Using format I tried different variations of:
But they all fail on the Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You've got the right idea, but with the golang templating you need to use the
For context, the format string is a plain golang $ echo '{
"apiVersion": "v1",
"items": [
{
"apiVersion": "networking.k8s.io/v1",
"kind": "Ingress",
"metadata": {
"annotations": {
"example": "value",
"kubectl.kubernetes.io/last-applied-configuration": "{\"escaped\":\"jsonValues\"}"
}
}
},
{
"apiVersion": "networking.k8s.io/v1",
"kind": "Ingress",
"metadata": {
"annotations": {
"example": "value"
}
}
}
]
}' | dasel -p json -m -s 'items.[*].metadata.annotations' --format '{{ if index . "kubectl.kubernetes.io/last-applied-configuration" }}{{ toJson . }}{{ else }}-{{ end}}'
{"example":"value","kubectl.kubernetes.io/last-applied-configuration":"{\"escaped\":\"jsonValues\"}"}
- https://dasel.tomwright.me/s/3b29e743-00f1-48f3-b3e6-884289502168 If you want pretty JSON there is a I can see the value in checking the presence of keys so perhaps that will come in the future. |
Beta Was this translation helpful? Give feedback.
You've got the right idea, but with the golang templating you need to use the
index
function to access map keys: https://pkg.go.dev/text/template#hdr-FunctionsFor context, the format string is a plain golang
text/template
and is processed outside of the normal dasel selector syntax. You could probably use theselect
func in the condition to use dasel syntax but in the meantime here's what I came up with: