JQ: mudanças entre as edições
Ir para navegação
Ir para pesquisar
Sem resumo de edição |
Sem resumo de edição |
||
Linha 14: | Linha 14: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Result | Result | ||
<syntaxhighlight lang= | <syntaxhighlight lang=json> | ||
{ | { | ||
"cluster": "MYCLUSTER", | "cluster": "MYCLUSTER", | ||
Linha 20: | Linha 20: | ||
"pod": "POD-XCHDF1" | "pod": "POD-XCHDF1" | ||
} | } | ||
</syntaxhighlight> | |||
=Flatten JSON= | |||
data.json | |||
<syntaxhighlight lang=json> | |||
{ | |||
"kubernetes": { | |||
"cluster": "MYCLUSTER", | |||
"namespace": "MYAPP-NAMESPACE", | |||
"pod": { | |||
"name": "POD-XCHDF1", | |||
"replicas": 5 | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
Command | |||
<syntaxhighlight lang=bash> | |||
cat data.json | jq -r 'paths(scalars) as $path | { ($path | map(tostring) | join("_")): getpath($path) }| to_entries|map("\(.key)=\(.value|tostring)")|.[]' | |||
</syntaxhighlight> | |||
Result | |||
<syntaxhighlight lang=bash> | |||
kubernetes_cluster=MYCLUSTER | |||
kubernetes_namespace=MYAPP-NAMESPACE | |||
kubernetes_pod_name=POD-XCHDF1 | |||
kubernetes_pod_replicas=5 | |||
</syntaxhighlight> | </syntaxhighlight> | ||
=Map Key and Values= | =Map Key and Values= |
Edição atual tal como às 18h13min de 29 de fevereiro de 2024
Links
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"
}
Flatten JSON
data.json
{
"kubernetes": {
"cluster": "MYCLUSTER",
"namespace": "MYAPP-NAMESPACE",
"pod": {
"name": "POD-XCHDF1",
"replicas": 5
}
}
}
Command
cat data.json | jq -r 'paths(scalars) as $path | { ($path | map(tostring) | join("_")): getpath($path) }| to_entries|map("\(.key)=\(.value|tostring)")|.[]'
Result
kubernetes_cluster=MYCLUSTER
kubernetes_namespace=MYAPP-NAMESPACE
kubernetes_pod_name=POD-XCHDF1
kubernetes_pod_replicas=5
Map Key and Values
echo '''
{
"cluster": "MYCLUSTER",
"namespace": "MYAPP-NAMESPACE",
"pod": "POD-XCHDF1"
}
'''|jq -r ' to_entries|map("\(.key)=\(.value|tostring)")|.[]'
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"