A flexible and lightweight command line JSON processor
jq is a lightweight and flexible command line JSON processor developed by stedolan. For the source code, please refer to jq project homepage
jq is used to process JSON input, apply a given filter to its JSON text input and generate the result of the filter as JSON on standard output.
The simplest filter is .
, which copies jq's input to its output unmodified (except for formatting).
Note that jq currently only supports 64-bit double-precision floating point numbers (IEEE754).
# Debian system, such as Ubuntu
sudo apt-get install jq
# RedHat system, such as CentOS
yum install jq
jq [options] <jq filter> [file...]
jq [options] --args <jq filter> [strings...]
jq [options] --jsonargs <jq filter> [JSON_TEXTS...]
-c compact rather than pretty output;
-n Use `null` as a single input value;
-e sets the exit status code based on the output;
-s reads (sips) all input into an array; applies filter;
-r outputs raw strings instead of JSON text;
-R reads raw strings instead of JSON text;
-C Colorize JSON;
-M monochrome (do not color JSON);
-S sorts objects by key on output;
--tab Use tab characters for indentation;
--arg a v Set variable $a to value<v>;
--argjson a v Set variable $a to JSON value<v>;
--slurpfile a f Set variable $a to a JSON text array read from <f>;
--rawfile a f Set variable $a to a string containing the content of <f>;
--args The remaining arguments are string arguments, not files;
--jsonargs The remaining parameters are JSON parameters, not files;
-- Terminate parameter processing;
.
: output in a pretty way
$ echo '{ "foo": { "bar": { "baz": 123 } } }' | jq '.'
{
"foo": {
"bar": {
"baz": 123
}
}
}
.foo, .foo.bar, .foo?
: Get the value of a key
$ echo '{"foo": 42, "bar": "less interesting data"}' | jq '.foo'
42
.[], .[]?, .[2], .[10:15]
: Array operations
$ echo '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]' | jq '.[1]'
{
"name": "XML",
"good": false
}
[], {}
: Construct an array/object
$ echo '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}' | jq '{user, title: .titles[]}'
{
"user": "stedolan",
"title": "JQ Primer"
}
{
"user": "stedolan",
"title": "More JQ"
}
length
: Calculate the length of a value
$ echo '[[1,2], "string", {"a":2}, null]' | jq '.[] | length'
2
6
1
0
keys
: Remove the keys from the array
$ echo '{"abc": 1, "abcd": 2, "Foo": 3}' | jq 'keys'
[
"Foo",
"abc",
"abcd"
]
,
: use multiple filters
$ echo '{ "foo": 42, "bar": "something else", "baz": true}' | jq '.foo, .bar'
42
"something else"
|
: Pipe the output of one filter as the input of the next filter
$ echo '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]' | jq '.[] | .name'
"JSON"
"XML"
select(foo)
: If foo returns true, the input remains unchanged
$ echo '[1,5,3,0,7]' | jq 'map(select(. >= 2))'
[
5,
3,
7
]
map(foo)
: calls the filter for each input
$ echo '[1,2,3]' | jq 'map(.+1)'
[
2,
3,
4
]
if-then-else-end
: conditional judgment
$ echo '2' | jq 'if . == 0 then "zero" elif . == 1 then "one" else "many" end'
"many"
\(foo)
: Insert values into strings and perform operations
$ echo '42' | jq '"The input was \(.), which is one less than \(.+1)"'
"The input was 42, which is one less than 43"