2015-08-10 10 views
6

Ad esempio, desidero estrarre i valori da una chiave, ma quella chiave a volte contiene un oggetto (intendo solo un valore) o talvolta contiene un array (cioè i valori multipli). Controlla se c'è un array o c'è un oggetto? Grazie.Come verificare se c'è un array o un oggetto in jq?

risposta

9

Utilizzare la funzione type:

type
La funzione tipo restituisce il tipo della sua tesi come stringa, che è uno dei nullo, booleano, numero, una stringa, un array o un oggetto.

Esempio 1:

echo '[0, false, [], {}, null, "hello"]' | jq 'map(type)' 
[ 
    "number", 
    "boolean", 
    "array", 
    "object", 
    "null", 
    "string" 
] 

Esempio 2:

echo '[0,1]' | jq 'if type=="array" then "yes" else "no" end' 
"yes" 

Esempio 3:

echo '{"0":0,"1":1}' | jq 'if type=="array" then "yes" else "no" end' 
"no" 
+0

Hi @Hans, Si può fornire l'esempio per array di elementi anche . [{"foo": null, "bar": "dati meno interessanti"}, {"foo": "null", "bar": "dati meno interessanti"}, {"foo": ["a", "b"], "bar": "dati meno interessanti"}]. Per verificare se foo è array o stringa. Sto usando sotto jq commond in jqPlay . [] | {"Result": if .foo == null then [] end} – devanathan

Problemi correlati