jq

jq, il processore JSON da riga di comando: filtra, trasforma e formatta file JSON, con le espressioni regolari. Scrivi jq '.nome' dati.json, oppure mandagli JSON con una pipe.

versione 1.8.2 · Licenza: MIT; Oniguruma BSD-2-Clause · Sito del progetto

jq

A cosa serve

jq è il coltellino svizzero del JSON: legge file JSON e ne estrae, filtra, trasforma e riformatta il contenuto con un piccolo linguaggio di filtri. Utile per capire al volo un file di configurazione, un export di dati o la risposta di un'API salvata con curl. Ci sono anche le espressioni regolari (test, match, sub, gsub, capture).

Come si usa

jq FILTRO file.json applica il filtro al file (o all'input della pipe se non dai il file):

Comando Cosa fa
jq . dati.json stampa il JSON indentato e leggibile
jq .nome dati.json il campo nome
jq -r '.app[].id' dati.json ogni id della lista app, come testo semplice
jq '.app[] \| select(.peso > 400)' dati.json solo gli elementi che rispettano la condizione
jq -c 'map(.peso) \| add' lista.json somma i pesi, risultato su una riga
jq --arg n lua '.app[] \| select(.id == $n)' dati.json usa un valore passato da fuori
jq -n '[range(5)] \| map(. * .)' senza input: calcola e basta
cat dati.json \| jq keys con una pipe della shell

Opzioni utili: -r testo senza virgolette, -c una riga per risultato, -s legge tutti i file come un'unica lista, -S ordina le chiavi, -f filtro.jq legge il filtro da un file.

Limiti

La riga di comando del Terminale non passa bene le virgolette doppie dentro un argomento: se il filtro contiene stringhe tra "...", scrivilo in un file e usa jq -f filtro.jq dati.json, oppure passa il valore con --arg. I colori ANSI si tolgono con -M. Memoria disponibile: fino a 8 MB (file JSON grandi, diciamo oltre 1-2 MB, possono non starci).

Nel Terminale

jq

jq, the command-line JSON processor: filter, transform and pretty-print JSON files, with regular expressions. Type jq '.name' data.json, or pipe JSON into it.

version 1.8.2 · License: MIT; Oniguruma BSD-2-Clause · Project site

jq

What it is

jq is the Swiss army knife for JSON: it reads JSON files and extracts, filters, transforms and reformats their content with a small filter language. Handy to make sense of a config file, a data export or an API response saved with curl. Regular expressions are there too (test, match, sub, gsub, capture).

How to use it

jq FILTER file.json applies the filter to the file (or to the pipe's input when no file is given):

Command What it does
jq . data.json prints the JSON indented and readable
jq .name data.json the name field
jq -r '.apps[].id' data.json every id of the apps list, as plain text
jq '.apps[] \| select(.size > 400)' data.json only the items matching the condition
jq -c 'map(.size) \| add' list.json sums the sizes, result on one line
jq --arg n lua '.apps[] \| select(.id == $n)' data.json uses a value passed from outside
jq -n '[range(5)] \| map(. * .)' no input: just compute
cat data.json \| jq keys through a shell pipe

Useful options: -r raw text without quotes, -c one line per result, -s reads all files as one list, -S sorts keys, -f filter.jq reads the filter from a file.

Limits

The Terminal's command line does not pass double quotes inside an argument well: if the filter has "..." strings, write it to a file and use jq -f filter.jq data.json, or pass the value with --arg. ANSI colours are turned off with -M. Memory: up to 8 MB (big JSON files, say over 1-2 MB, may not fit).

In the Terminal