JQ: mudanças entre as edições

De Wiki Clusterlab.com.br
Ir para navegação Ir para pesquisar
Sem resumo de edição
Sem resumo de edição
 
(Uma revisão intermediária pelo mesmo usuário não está sendo mostrada)
Linha 1: Linha 1:
*[https://jqlang.github.io/jq/manual/ JQ Lang Mnual]
=Links=
*[https://jqlang.github.io/jq/manual/ JQ Lang Manual]
=Create JSON=
=Create JSON=
Command
Command
Linha 13: Linha 14:
</syntaxhighlight>
</syntaxhighlight>
Result
Result
<syntaxhighlight lang=bash>
<syntaxhighlight lang=json>
{
{
   "cluster": "MYCLUSTER",
   "cluster": "MYCLUSTER",
Linha 19: 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"