JQ: mudanças entre as edições
Ir para navegação
Ir para pesquisar
(Criou página com '=Create JSON= Command <syntaxhighlight lang=bash> export CLUSTER=MYCLUSTER export NAMESPACE=MYAPP-NAMESPACE export POD=POD-XCHDF1 jq --null-input \ --arg cluster "$CLU...') |
Sem resumo de edição |
||
Linha 12: | Linha 12: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Result | Result | ||
<syntaxhighlight lang= | <syntaxhighlight lang=bash> | ||
{ | |||
"cluster": "MYCLUSTER", | |||
"namespace": "MYAPP-NAMESPACE", | |||
"pod": "POD-XCHDF1" | |||
} | |||
</syntaxhighlight> | |||
=Test if key is Not Null= | |||
Command | |||
<syntaxhighlight lang=bash> | |||
$ echo ''' | |||
{ | { | ||
"cluster": "MYCLUSTER", | "cluster": "MYCLUSTER", | ||
"namespace": "MYAPP-NAMESPACE", | "namespace": "MYAPP-NAMESPACE", | ||
"pod": "POD-XCHDF1" | "pod": "POD-XCHDF1" | ||
} | |||
''' | jq -r '{"cluster": (.cluster| . |= if .==null then "NOK" else "OK" end),"deployment":( .deployment|. |= if .==null then "NOK" else "OK" end)}' | |||
</syntaxhighlight> | |||
Result | |||
<syntaxhighlight lang=json> | |||
{ | |||
"cluster": "OK", | |||
"deployment": "NOK" | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Edição das 13h47min de 30 de janeiro de 2024
Create JSON
Command
export CLUSTER=MYCLUSTER
export NAMESPACE=MYAPP-NAMESPACE
export POD=POD-XCHDF1
jq --null-input \
--arg cluster "$CLUSTER" \
--arg namespace "$NAMESPACE" \
--arg pod "$POD" \
'{"cluster": $cluster, "namespace": $namespace, "pod": $pod}'
Result
{
"cluster": "MYCLUSTER",
"namespace": "MYAPP-NAMESPACE",
"pod": "POD-XCHDF1"
}
Test if key is Not Null
Command
$ echo '''
{
"cluster": "MYCLUSTER",
"namespace": "MYAPP-NAMESPACE",
"pod": "POD-XCHDF1"
}
''' | jq -r '{"cluster": (.cluster| . |= if .==null then "NOK" else "OK" end),"deployment":( .deployment|. |= if .==null then "NOK" else "OK" end)}'
Result
{
"cluster": "OK",
"deployment": "NOK"
}
To CSV
Command
function GEN_JSON() {
for i in $(seq 1 10)
do
export CLUSTER=MYCLUSTER
export NAMESPACE=MYAPP-NAMESPACE-${i}
export POD=POD-${i}-XCHDF1
jq --null-input \
--arg cluster "$CLUSTER" \
--arg namespace "$NAMESPACE" \
--arg pod "$POD" \
'{"cluster": $cluster, "namespace": $namespace, "pod": $pod}'
done
}
$ GEN_JSON | jq -r '.|map(.)|@csv'
Result
"MYCLUSTER","MYAPP-NAMESPACE-1","POD-1-XCHDF1"
"MYCLUSTER","MYAPP-NAMESPACE-2","POD-2-XCHDF1"
"MYCLUSTER","MYAPP-NAMESPACE-3","POD-3-XCHDF1"
"MYCLUSTER","MYAPP-NAMESPACE-4","POD-4-XCHDF1"
"MYCLUSTER","MYAPP-NAMESPACE-5","POD-5-XCHDF1"
"MYCLUSTER","MYAPP-NAMESPACE-6","POD-6-XCHDF1"
"MYCLUSTER","MYAPP-NAMESPACE-7","POD-7-XCHDF1"
"MYCLUSTER","MYAPP-NAMESPACE-8","POD-8-XCHDF1"
"MYCLUSTER","MYAPP-NAMESPACE-9","POD-9-XCHDF1"
"MYCLUSTER","MYAPP-NAMESPACE-10","POD-10-XCHDF1"