JQ
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"
}
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"