Jack Pearkes <jackpearkes@gmail.com> - update tutorial
Jingwen Owen Ou <jingweno@gmail.com> - jqplay.org and link to it
Joe Littlejohn <joe.littlejohn@nokia.com> - bug fixes
-Juan Guerrero <juan.guerrero.lozano@gmail.com> - bug fixes
+Juan Guerrero <juan.guerrero.lozano@gmail.com> - bug fixes
Kenny Shen <kenny.shen@zalora.com> - doc fixes
Lee Thompson <stagr.lee@gmail.com> - autoconf stuff
Markus Lanthaler <mark_lanthaler@gmx.net> - doc fixes
The documentation website includes a copy of Twitter's Boostrap and
relies on Bonsai, Liquid templates and various other projects, look
them up for detailed licensing conditions.
-
-
+
+
jq incorporates David M. Gay's dtoa.c and g_fmt.c, which bear the
following notices:
$(AM_V_LEX) flex -o lexer.c --header-file=lexer.h $<
lexer.h: lexer.c
else
-BUILT_SOURCES =
+BUILT_SOURCES =
.y.c:
$(AM_V_YACC) echo "NOT building parser.c!"
.l.c:
docs/default_manpage.md jq.1.default
# setup is only used by distribution developers, not package developers.
-# Still, as a matter of allowing patching, its not a bad idea to distribute
+# Still, as a matter of allowing patching, its not a bad idea to distribute
# the developer setup script in the tarball.
EXTRA_DIST = $(man_MANS) $(TESTS) $(TEST_LOG_COMPILER) jq.spec \
$(DOC_FILES) scripts/version parser.h parser.c lexer.h \
jq.1.prebuilt
-# README.md is expected in Github projects, good stuff in it, so we'll
+# README.md is expected in Github projects, good stuff in it, so we'll
# distribute it and install it with the package in the doc directory.
docdir = ${datadir}/doc/${PACKAGE}
dist_doc_DATA = README.md COPYING AUTHORS README
clean-local-docs:
if ENABLE_DOCS
rm -f jq.1
-endif
+endif
clean-local-gcov:
rm -f *.gcno *.gcda *.gcov
static __thread int x ;
int main () { x = 123; return x; }
])], have___thread=yes)
-if test $have___thread = yes; then
+if test $have___thread = yes; then
AC_DEFINE([HAVE___THREAD],1,[Define to 1 if the system supports __thread])
fi
AC_MSG_RESULT($have___thread)
def json(input)
input.to_json
end
-
+
def unique(input)
@n = (@n || 0) + 1
input + @n.to_s
task :serve do
begin
Bonsai.log "Press Control+C to quit"
-
+
require 'rack'
require 'sinatra'
require 'watch'
require 'launchy'
-
+
Bonsai.root_dir = Dir.pwd
-
- server = fork {
+
+ server = fork {
app = Rack::Builder.app {
map "/jq" do
use Bonsai::StaticPassThrough, :root => Bonsai.root_dir + "/output", :urls => ["/"]
Rack::Handler.default.run(app, :Port => 5000) do
Launchy.open("http://localhost:5000/jq/")
end
- }
+ }
Watch.new("{content,templates,public}/**/*") { Bonsai::Exporter.process! }
rescue Interrupt
Process.kill("QUIT", server)
f.puts manual['manpage_intro']
f.puts manual['body']
manual['sections'].each do |section|
-
+
f.puts "## #{section['title'].upcase}\n"
f.puts section['body']
f.puts ""
f.puts manual['manpage_epilogue']
f.close
puts Ronn::Document.new(f.path).convert('roff').gsub(/<\/?code>/,"")
- end
+ end
end
task :manpage_default => ["default_manpage.md"] do
### Linux
-
+
* jq 1.4 is in the official [Debian](http://packages.debian.org/jq) and
[Ubuntu](http://packages.ubuntu.com/jq) repositories. Install using
`sudo apt-get install jq`.
1.5, neither is required if you use `./configure
--disable-maintainer-mode` instead of `./configure`. This flag says to use
the pre-generated lexer and parser that come with the code.
-
+
#### Building the documentation
jq's documentation is compiled into static HTML using
that string that many times. `"x" * 0` produces **null**.
Dividing a string by another splits the first using the second
- as separators.
+ as separators.
Multiplying two objects will merge them recursively: this works
like addition but if both objects contain a value for the
- title: "`in`"
body: |
-
+
The builtin function `in` returns the input key is in the
given object, or the input index corresponds to an element
in the given array. It is, essentially, an inversed version
of `has`.
-
+
examples:
- program: '.[] | in({"foo": 42})'
input: '["foo", "bar"]'
- program: 'map(in([0,1]))'
input: '[2, 0]'
output: ['[false, true]']
-
+
- title: "`path(path_expression)`"
body: |
For any filter `x`, `map(x)` will run that filter for each
element of the input array, and produce the outputs a new
array. `map(.+1)` will increment each element of an array of numbers.
-
+
Similarly, `map_values(x)` will run that filter for each element,
but it will return an object when an object is passed.
-
+
`map(x)` is equivalent to `[.[] | x]`. In fact, this is how
it's defined. Similarly, `map_values(x)` is defined as `.[] |= x`.
- program: 'map(.+1)'
input: '[1,2,3]'
output: ['[2,3,4]']
-
+
- program: 'map_values(.+1)'
input: '{"a": 1, "b": 2, "c": 3}'
output: ['{"a": 2, "b": 3, "c": 4}']
- title: "`ascii_downcase`, `ascii_upcase`"
body: |
- Emit a copy of the input string with its alphabetic characters (a-z and A-Z)
+ Emit a copy of the input string with its alphabetic characters (a-z and A-Z)
converted to the specified case.
example:
- title: "`transpose`"
body: |
- Transpose a possibly jagged matrix (an array of arrays).
+ Transpose a possibly jagged matrix (an array of arrays).
Rows are padded with nulls so the result is always rectangular.
examples:
However, streaming isn't easy to deal with as the jq program will
have `[<path>, <leaf-value>]` (and a few other forms) as inputs.
-
+
Several builtins are provided to make handling streams easier.
The examples below use the the streamed form of `[0,[1]]`, which
this time jq only uses the "search" key/value of the metadata.
The metadata is also made available to users via the
`modulemeta` builtin.
-
+
The "search" key in the metadata, if present, should have a
string or array value (array of strings); this is the search
path to be prefixed to the top-level search path.
this time jq only uses the "search" key/value of the metadata.
The metadata is also made available to users via the
`modulemeta` builtin.
-
+
The "search" key in the metadata, if present, should have a
string or array value (array of strings); this is the search
path to be prefixed to the top-level search path.
Filters can be combined in various ways - you can pipe the output of
one filter into another filter, or collect the output of a filter
into an array.
-
+
Some filters produce multiple results, for instance there's one that
produces all the elements of its input array. Piping that filter
into a second runs the second filter for each element of the
By default, `jq` reads a stream of JSON objects (whitespace
separated) from `stdin`. One or more <files> may be specified, in
which case `jq` will read input from those instead.
-
+
The <options> are described in the [INVOKING JQ] section, they
mostly concern input and output formatting. The <filter> is written
in the jq language and specifies how to transform the input
## AUTHOR
Stephen Dolan `<mu@netsoc.tcd.ie>`
-
+
sections:
- title: Invoking jq
body: |
-
+
jq filters run on a stream of JSON data. The input to jq is
parsed as a sequence of whitespace-separated JSON values which
are passed through the provided filter one at a time. The
using some command-line options:
* `--slurp`/`-s`:
-
+
Instead of running the filter for each JSON object in the
input, read the entire input stream into a large array and run
the filter just once.
-
+
* `--raw-input`/`-R`:
-
+
Don't parse the input as JSON. Instead, each line of text is
passed to the filter as a string. If combined with `--slurp`,
then the entire input is passed to the filter as a single long
string.
-
+
* `--null-input`/`-n`:
-
+
Don't read any input at all! Instead, the filter is run once
using `null` as the input. This is useful when using jq as a
simple calculator or to construct JSON data from scratch.
-
+
* `--compact-output` / `-c`:
-
+
By default, jq pretty-prints JSON output. Using this option
will result in more compact output by instead putting each
JSON object on a single line.
-
+
* `--colour-output` / `-C` and `--monochrome-output` / `-M`:
-
+
By default, jq outputs colored JSON if writing to a
terminal. You can force it to produce color even if writing to
a pipe or a file using `-C`, and disable color with `-M`.
-
+
* `--ascii-output` / `-a`:
-
+
jq usually outputs non-ASCII Unicode codepoints as UTF-8, even
if the input specified them as escape sequences (like
"\u03bc"). Using this option, you can force jq to produce pure
ASCII output with every non-ASCII character replaced with the
equivalent escape sequence.
-
+
* `--raw-output` / `-r`:
-
+
With this option, if the filter's result is a string then it
will be written directly to standard output rather than being
formatted as a JSON string with quotes. This can be useful for
entries:
- title: "`.`"
body: |
-
+
The absolute simplest (and least interesting) filter
is `.`. This is a filter that takes its input and
produces it unchanged as output.
- title: "`.foo`"
body: |
-
+
The simplest *useful* filter is .foo. When given a
JSON object (aka dictionary or hash) as input, it produces
the value at the key "foo", or null if there's none present.
- title: "`.[foo]`, `.[2]`, `.[10:15]`"
body: |
-
+
You can also look up fields of an object using syntax like
`.["foo"]` (.foo above is a shorthand version of this). This
one works for arrays as well, if the key is an
- program: '.[2:4]'
input: '["a","b","c","d","e"]'
output: ['["c", "d"]']
-
+
- program: '.[:3]'
input: '["a","b","c","d","e"]'
output: ['["a", "b", "c"]']
- title: "`.[]`"
body: |
-
+
If you use the `.[foo]` syntax, but omit the index
entirely, it will return *all* of the elements of an
array. Running `.[]` with the input `[1,2,3]` will produce the
- title: "`,`"
body: |
-
+
If two filters are separated by a comma, then the
input will be fed into both and there will be multiple
outputs: first, all of the outputs produced by the left
- program: ".user, .projects[]"
input: '{"user":"stedolan", "projects": ["jq", "wikiflow"]}'
- output: ['"stedolan"', '"jq"', '"wikiflow"']
-
+ output: ['"stedolan"', '"jq"', '"wikiflow"']
+
- program: '.[4,2]'
input: '["a","b","c","d","e"]'
output: ['"e"', '"c"']
-
+
- title: "`|`"
body: |
The | operator combines two filters by feeding the output(s) of
the one on the left into the input of the one on the right. It's
pretty much the same as the Unix shell's pipe, if you're used to
- that.
+ that.
If the one on the left produces multiple results, the one on
the right will be run for each of those results. So, the
- title: Types and Values
body: |
-
+
jq supports the same set of datatypes as JSON - numbers,
strings, booleans, arrays, objects (which in JSON-speak are
hashes with only string keys), and "null".
entries:
- title: Array construction - `[]`
body: |
-
+
As in JSON, `[]` is used to construct arrays, as in
`[1,2,3]`. The elements of the arrays can be any jq
expression. All of the results produced by all of the
to construct an array out of a known quantity of values (as
in `[.foo, .bar, .baz]`) or to "collect" all the results of a
filter into an array (as in `[.items[].name]`)
-
+
Once you understand the "," operator, you can look at jq's array
syntax in a different light: the expression `[1,2,3]` is not using a
built-in syntax for comma-separated arrays, but is instead applying
Like JSON, `{}` is for constructing objects (aka
dictionaries or hashes), as in: `{"a": 42, "b": 17}`.
-
+
If the keys are "sensible" (all alphabetic characters), then
the quotes can be left off. The value can be any expression
(although you may need to wrap it in parentheses if it's a
complicated one), which gets applied to the {} expression's
input (remember, all filters have an input and an
output).
-
+
{foo: .bar}
-
+
will produce the JSON object `{"foo": 42}` if given the JSON
object `{"bar":42, "baz":43}`. You can use this to select
particular fields of an object: if the input is an object
with "user", "title", "id", and "content" fields and you
just want "user" and "title", you can write
-
+
{user: .user, title: .title}
-
+
Because that's so common, there's a shortcut syntax: `{user, title}`.
-
+
If one of the expressions produces multiple results,
multiple dictionaries will be produced. If the input's
-
+
{"user":"stedolan","titles":["JQ Primer", "More JQ"]}
-
+
then the expression
-
+
{user, title: .titles[]}
-
- will produce two outputs:
-
+
+ will produce two outputs:
+
{"user":"stedolan", "title": "JQ Primer"}
{"user":"stedolan", "title": "More JQ"}
-
+
Putting parentheses around the key means it will be evaluated as an
expression. With the same input as above,
-
+
{(.user): .titles}
-
+
produces
-
+
{"stedolan": ["JQ Primer", "More JQ"]}
-
+
examples:
- program: '{user, title: .titles[]}'
input: '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}'
etc.). However, jq never does implicit type conversions. If you
try to add a string to an object you'll get an error message and
no result.
-
+
entries:
- title: Addition - `+`
body: |
- **Arrays** are added by being concatenated into a larger array.
- **Strings** are added by being joined into a larger string.
-
+
- **Objects** are added by merging, that is, inserting all
the key-value pairs from both objects into a single
combined object. If both objects contain a value for the
same key, the object on the right of the `+` wins.
-
+
`null` can be added to any value, and returns the other
value unchanged.
- title: `keys`
body: |
-
+
The builtin function `keys`, when given an object, returns
its keys in an array.
- title: `has`
body: |
-
+
The builtin function `has` returns whether the input object
has the given key, or the input array has an element at the
given index.
- title: `to_entries`, `from_entries`, `with_entries`
body: |
-
+
These functions convert between an object and an array of
key-value pairs. If `to_entries` is passed an object, then
for each `k: v` entry in the input, the output array
- includes `{"key": k, "value": v}`.
+ includes `{"key": k, "value": v}`.
`from_entries` does the opposite conversion, and
`with_entries(foo)` is a shorthand for `to_entries |
- title: `select`
body: |
-
+
The function `select(foo)` produces its input unchanged if
`foo` returns true for that input, and produces no output
otherwise.
- title: `empty`
body: |
-
+
`empty` returns no results. None at all. Not even `null`.
It's useful on occasion. You'll know if you need it :)
- title: `range`
body: |
-
+
The `range` function produces a range of numbers. `range(4;10)`
produces 6 numbers, from 4 (inclusive) to 10 (exclusive). The numbers
- are produced as separate outputs. Use `[range(4;10)]` to get a range as
+ are produced as separate outputs. Use `[range(4;10)]` to get a range as
an array.
examples:
- program: '[range(2;4)]'
input: 'null'
output: ['[2,3]']
-
+
- title: `tonumber`
body: |
- title: `type`
body: |
-
+
The `type` function returns the type of its argument as a
string, which is one of null, boolean, number, string, array
or object.
- title: `sort, sort_by`
body: |
-
+
The `sort` functions sorts its input, which must be an
array. Values are sorted in the following order:
- title: `group_by`
body: |
-
+
`group_by(.foo)` takes as input an array, groups the
elements having the same `.foo` field into separate arrays,
and produces all of these arrays as elements of a larger
- title: `min`, `max`, `min_by`, `max_by`
body: |
-
+
Find the minimum or maximum element of the input array. The
`_by` versions allow you to specify a particular field or
property to examine, e.g. `min_by(.foo)` finds the object
- title: `unique`
body: |
-
+
The `unique` function takes as input an array and produces
an array of the same elements, in sorted order, with
duplicates removed.
- title: `reverse`
body: |
-
+
This function reverses an array.
examples:
- title: `recurse`
body: |
-
+
The `recurse` function allows you to search through a
recursive structure, and extract interesting data from all
levels. Suppose your input represents a filesystem:
{"name": "/home", "children": [
{"name": "/home/stephen", "children": [
{"name": "/home/stephen/jq", "children": []}]}]}]}
-
+
Now suppose you want to extract all of the filenames
present. You need to retrieve `.name`, `.children[].name`,
`.children[].children[].name`, and so on. You can do this
examples:
- program: 'recurse(.foo[])'
input: '{"foo":[{"foo": []}, {"foo":[{"foo":[]}]}]}'
- output:
+ output:
- '{"foo":[{"foo":[]},{"foo":[{"foo":[]}]}]}'
- '{"foo":[]}'
- '{"foo":[{"foo":[]}]}'
- program: '"The input was \(.), which is one less than \(.+1)"'
input: '42'
output: ['"The input was 42, which is one less than 43"']
-
+
- title: "Format strings and escaping"
body: |
characters to a `%xx` sequence.
* `@csv`:
-
+
The input must be an array, and it is rendered as CSV
with double quotes for strings, and quotes escaped by
repetition.
* `@sh`:
-
+
The input is escaped suitable for use in a command-line
for a POSIX shell. If the input is an array, the output
will be a series of space-separated strings.
-
+
* `@base64`:
The input is converted to base64 as specified by RFC 4648.
- program: '@sh "echo \(.)"'
input: "\"O'Hara's Ale\""
output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]
-
+
- title: Conditionals and Comparisons
entries:
- title: `==`, `!=`
to numbers. If you're coming from Javascript, jq's == is like
Javascript's === - considering values equal only when they have the
same type as well as the same value.
-
+
!= is "not equal", and 'a != b' returns the opposite value of 'a == b'
examples:
`if A then B else C end` will act the same as `B` if `A`
produces a value other than false or null, but act the same
as `C` otherwise.
-
+
Checking for false or null is a simpler notion of
"truthiness" than is found in Javascript or Python, but it
means that you'll sometimes have to be more explicit about
string is empty using `if .name then A else B end`, you'll
need something more like `if (.name | count) > 0 then A else
B end` instead.
-
+
If the condition A produces multiple results, it is
considered "true" if any of those results is not false or
null. If it produces zero results, it's considered false.
-
+
More cases can be added to an if using `elif A then B` syntax.
examples:
end
input: 2
output: ['"many"']
-
+
- title: `>, >=, <=, <`
body: |
-
+
The comparison operators `>`, `>=`, `<=`, `<` return whether
their left argument is greater than, greater than or equal
to, less than or equal to or less than their right argument
so it is called as a filter to which things can be piped
rather than with special syntax, as in `.foo and .bar |
not`.
-
+
These three only produce the values "true" and "false", and
so are only useful for genuine Boolean operations, rather
than the common Perl/Python/Ruby idiom of
"value_that_may_be_null or default". If you want to use this
form of "or", picking between two values rather than
evaluating a condition, see the "//" operator below.
-
+
examples:
- program: '42 and "a string"'
input: 'null'
- program: '.foo // 42'
input: '{}'
output: [42]
-
+
- title: Advanced features
body: |
Variables are an absolute necessity in most programming languages, but
entries:
- title: Variables
body: |
-
+
In jq, all filters have an input and an output, so manual
plumbing is not necessary to pass a value from one part of a program
to the next. Many expressions, for instance `a + b`, pass their input
to two distinct subexpressions (here `a` and `b` are both passed the
same input), so variables aren't usually necessary in order to use a
value twice.
-
+
For instance, calculating the average value of an array of numbers
requires a few variables in most languages - at least one to hold the
array, perhaps one for each element or for a loop counter. In jq, it's
simply `add / length` - the `add` expression is given the array and
produces its sum, and the `length` expression is given the array and
produces its length.
-
+
So, there's generally a cleaner way to solve most problems in jq that
defining variables. Still, sometimes they do make things easier, so jq
lets you define variables using `expression as $variable`. All
variable names start with `$`. Here's a slightly uglier version of the
array-averaging example:
-
+
length as $array_length | add / $array_length
-
+
We'll need a more complicated problem to find a situation where using
variables actually makes our lives easier.
-
-
+
+
Suppose we have an array of blog posts, with "author" and "title"
fields, and another object which is used to map author usernames to
real names. Our input looks like:
-
+
{"posts": [{"title": "Frist psot", "author": "anon"},
{"title": "A well-written article", "author": "person1"}],
"realnames": {"anon": "Anonymous Coward",
"person1": "Person McPherson"}}
-
+
We want to produce the posts with the author field containing a real
name, as in:
-
+
{"title": "Frist psot", "author": "Anonymous Coward"}
{"title": "A well-written article", "author": "Person McPherson"}
-
+
We use a variable, $names, to store the realnames object, so that we
can refer to it later when looking up author usernames:
-
+
.realnames as $names | .posts[] | {title, author: $names[.author]}
-
+
The expression `exp as $x | ...` means: for each value of expression
`exp`, run the rest of the pipeline with the entire original input, and
- with `$x` set to that value. Thus `as` functions as something of a
+ with `$x` set to that value. Thus `as` functions as something of a
foreach loop.
Variables are scoped over the rest of the expression that defines
- them, so
-
+ them, so
+
.realnames as $names | (.posts[] | {title, author: $names[.author]})
-
- will work, but
-
+
+ will work, but
+
(.realnames as $names | .posts[]) | {title, author: $names[.author]}
-
+
won't.
examples:
- title: 'Defining Functions'
body: |
-
+
You can give a filter a name using "def" syntax:
def increment: . + 1;
-
+
From then on, `increment` is usable as a filter just like a
builtin function (in fact, this is how some of the builtins
are defined). A function may take arguments:
- title: Reduce
body: |
-
+
The `reduce` syntax in jq allows you to combine all of the
results of an expression by accumulating them into a single
answer. As an example, we'll pass `[3,2,1]` to this expression:
reduce .[] as $item (0; . + $item)
-
+
For each result that `.[]` produces, `. + $item` is run to
accumulate a running total, starting from 0. In this
example, `.[]` produces the results 3, 2, and 1, so the
effect is similar to running something like this:
- 0 | (3 as $item | . + $item) |
+ 0 | (3 as $item | . + $item) |
(2 as $item | . + $item) |
(1 as $item | . + $item)
- program: 'reduce .[] as $item (0; . + $item)'
input: '[10,2,5,3]'
output: ['20']
-
+
- title: Assignment
body: |
entries:
- title: "`=`"
body: |
-
+
The filter `.foo = 1` will take as input an object
and produce as output an object with the "foo" field set to
1. There is no notion of "modifying" or "changing" something
in jq - all jq values are immutable. For instance,
-
+
.foo = .bar | .foo.baz = 1
-
+
will not have the side-effect of setting .bar.baz to be set
to 1, as the similar-looking program in Javascript, Python,
Ruby or other languages would. Unlike these languages (but
"the same object". They can be equal, or not equal, but if
we change one of them in no circumstances will the other
change behind our backs.
-
+
This means that it's impossible to build circular values in
jq (such as an array whose first element is itself). This is
quite intentional, and ensures that anything a jq program
works out the new value for the property being assigned to by running
the old value through this expression. For instance, .foo |= .+1 will
build an object with the "foo" field set to the input's "foo" plus 1.
-
+
This example should show the difference between '=' and '|=':
-
+
Provide input '{"a": {"b": 10}, "b": 20}' to the programs:
-
+
.a = .b
.a |= .b
-
+
The former will set the "a" field of the input to the "b" field of the
input, and produce the output {"a": 20}. The latter will set the "a"
field of the input to the "a" field's "b" field, producing {"a": 10}.
- program: .foo += 1
input: '{"foo": 42}'
output: ['{"foo": 43}']
-
+
- title: Complex assignments
body: |
Lots more things are allowed on the left-hand side of a jq assignment
than in most langauges. We've already seen simple field accesses on
the left hand side, and it's no surprise that array accesses work just
as well:
-
+
.posts[0].title = "JQ Manual"
-
+
What may come as a surprise is that the expression on the left may
produce multiple results, referring to different points in the input
document:
-
+
.posts[].comments |= . + ["this is great"]
-
+
That example appends the string "this is great" to the "comments"
array of each post in the input (where the input is an object with a
field "posts" which is an array of posts).
-
+
When jq encounters an assignment like 'a = b', it records the "path"
taken to select a part of the input document while executing a. This
path is then used to find which part of the input to change while
executing the assignment. Any filter may be used on the
left-hand side of an equals - whichever paths it selects from the
input will be where the assignment is performed.
-
+
This is a very powerful operation. Suppose we wanted to add a comment
to blog posts, using the same "blog" input above. This time, we only
want to comment on the posts written by "stedolan". We can find those
posts using the "select" function described earlier:
-
+
.posts[] | select(.author == "stedolan")
-
+
The paths provided by this operation point to each of the posts that
"stedolan" wrote, and we can comment on each of them in the same way
that we did before:
-
+
(.posts[] | select(.author == "stedolan") | .comments) |=
. + ["terrible."]
Filters can be combined in various ways - you can pipe the output of
one filter into another filter, or collect the output of a filter
into an array.
-
+
Some filters produce multiple results, for instance there's one that
produces all the elements of its input array. Piping that filter
into a second runs the second filter for each element of the
By default, `jq` reads a stream of JSON objects (whitespace
separated) from `stdin`. One or more <files> may be specified, in
which case `jq` will read input from those instead.
-
+
The <options> are described in the [INVOKING JQ] section, they
mostly concern input and output formatting. The <filter> is written
in the jq language and specifies how to transform the input
## AUTHOR
Stephen Dolan `<mu@netsoc.tcd.ie>`
-
+
sections:
- title: Invoking jq
body: |
-
+
jq filters run on a stream of JSON data. The input to jq is
parsed as a sequence of whitespace-separated JSON values which
are passed through the provided filter one at a time. The
Output the jq version and exit with zero.
* `--slurp`/`-s`:
-
+
Instead of running the filter for each JSON object in the
input, read the entire input stream into a large array and run
the filter just once.
* `--online-input`/`-I`:
-
+
When the top-level input value is an array produce its elements
instead of the array. This allows on-line processing of
potentially very large top-level arrays' elements.
-
+
* `--raw-input`/`-R`:
-
+
Don't parse the input as JSON. Instead, each line of text is
passed to the filter as a string. If combined with `--slurp`,
then the entire input is passed to the filter as a single long
string.
-
+
* `--null-input`/`-n`:
-
+
Don't read any input at all! Instead, the filter is run once
using `null` as the input. This is useful when using jq as a
simple calculator or to construct JSON data from scratch.
-
+
* `--compact-output` / `-c`:
-
+
By default, jq pretty-prints JSON output. Using this option
will result in more compact output by instead putting each
JSON object on a single line.
-
+
* `--colour-output` / `-C` and `--monochrome-output` / `-M`:
-
+
By default, jq outputs colored JSON if writing to a
terminal. You can force it to produce color even if writing to
a pipe or a file using `-C`, and disable color with `-M`.
-
+
* `--ascii-output` / `-a`:
-
+
jq usually outputs non-ASCII Unicode codepoints as UTF-8, even
if the input specified them as escape sequences (like
"\u03bc"). Using this option, you can force jq to produce pure
output elsewhere).
* `--sort-keys` / `-S`:
-
+
Output the fields of each object with the keys in sorted order.
* `--raw-output` / `-r`:
-
+
With this option, if the filter's result is a string then it
will be written directly to standard output rather than being
formatted as a JSON string with quotes. This can be useful for
entries:
- title: "`.`"
body: |
-
+
The absolute simplest (and least interesting) filter
is `.`. This is a filter that takes its input and
produces it unchanged as output.
- title: "`.foo`, `.foo.bar`"
body: |
-
+
The simplest *useful* filter is `.foo`. When given a
JSON object (aka dictionary or hash) as input, it produces
the value at the key "foo", or null if there's none present.
it with double quotes like this: `."foo$"`.
A filter of the form `.foo.bar` is equivalent to `.foo|.bar`.
-
+
examples:
- program: '.foo'
input: '{"foo": 42, "bar": "less interesting data"}'
- title: "`.foo?`"
body: |
-
+
Just like `.foo`, but does not output even an error when `.`
is not an array or an object.
- title: "`.[<string>]`, `.[2]`, `.[10:15]`"
body: |
-
+
You can also look up fields of an object using syntax like
`.["foo"]` (.foo above is a shorthand version of this). This
one works for arrays as well, if the key is an
- program: '.[2:4]'
input: '"abcdefghi"'
output: ['"cd"']
-
+
- program: '.[:3]'
input: '["a","b","c","d","e"]'
output: ['["a", "b", "c"]']
- title: "`.[]`"
body: |
-
+
If you use the `.[index]` syntax, but omit the index
entirely, it will return *all* of the elements of an
array. Running `.[]` with the input `[1,2,3]` will produce the
- title: "`.[]?`"
body: |
-
+
Like `.[]`, but no errors will be output if . is not an array
or object.
- title: "`,`"
body: |
-
+
If two filters are separated by a comma, then the
input will be fed into both and there will be multiple
outputs: first, all of the outputs produced by the left
- program: ".user, .projects[]"
input: '{"user":"stedolan", "projects": ["jq", "wikiflow"]}'
- output: ['"stedolan"', '"jq"', '"wikiflow"']
-
+ output: ['"stedolan"', '"jq"', '"wikiflow"']
+
- program: '.[4,2]'
input: '["a","b","c","d","e"]'
output: ['"e"', '"c"']
-
+
- title: "`|`"
body: |
The | operator combines two filters by feeding the output(s) of
the one on the left into the input of the one on the right. It's
pretty much the same as the Unix shell's pipe, if you're used to
- that.
+ that.
If the one on the left produces multiple results, the one on
the right will be run for each of those results. So, the
- title: Types and Values
body: |
-
+
jq supports the same set of datatypes as JSON - numbers,
strings, booleans, arrays, objects (which in JSON-speak are
hashes with only string keys), and "null".
entries:
- title: Array construction - `[]`
body: |
-
+
As in JSON, `[]` is used to construct arrays, as in
`[1,2,3]`. The elements of the arrays can be any jq
expression. All of the results produced by all of the
to construct an array out of a known quantity of values (as
in `[.foo, .bar, .baz]`) or to "collect" all the results of a
filter into an array (as in `[.items[].name]`)
-
+
Once you understand the "," operator, you can look at jq's array
syntax in a different light: the expression `[1,2,3]` is not using a
built-in syntax for comma-separated arrays, but is instead applying
Like JSON, `{}` is for constructing objects (aka
dictionaries or hashes), as in: `{"a": 42, "b": 17}`.
-
+
If the keys are "sensible" (all alphabetic characters), then
the quotes can be left off. The value can be any expression
(although you may need to wrap it in parentheses if it's a
complicated one), which gets applied to the {} expression's
input (remember, all filters have an input and an
output).
-
+
{foo: .bar}
-
+
will produce the JSON object `{"foo": 42}` if given the JSON
object `{"bar":42, "baz":43}`. You can use this to select
particular fields of an object: if the input is an object
with "user", "title", "id", and "content" fields and you
just want "user" and "title", you can write
-
+
{user: .user, title: .title}
-
+
Because that's so common, there's a shortcut syntax: `{user, title}`.
-
+
If one of the expressions produces multiple results,
multiple dictionaries will be produced. If the input's
-
+
{"user":"stedolan","titles":["JQ Primer", "More JQ"]}
-
+
then the expression
-
+
{user, title: .titles[]}
-
- will produce two outputs:
-
+
+ will produce two outputs:
+
{"user":"stedolan", "title": "JQ Primer"}
{"user":"stedolan", "title": "More JQ"}
-
+
Putting parentheses around the key means it will be evaluated as an
expression. With the same input as above,
-
+
{(.user): .titles}
-
+
produces
-
+
{"stedolan": ["JQ Primer", "More JQ"]}
-
+
examples:
- program: '{user, title: .titles[]}'
input: '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}'
etc.). However, jq never does implicit type conversions. If you
try to add a string to an object you'll get an error message and
no result.
-
+
entries:
- title: Addition - `+`
body: |
- **Arrays** are added by being concatenated into a larger array.
- **Strings** are added by being joined into a larger string.
-
+
- **Objects** are added by merging, that is, inserting all
the key-value pairs from both objects into a single
combined object. If both objects contain a value for the
same key, the object on the right of the `+` wins. (For
recursive merge use the `*` operator.)
-
+
`null` can be added to any value, and returns the other
value unchanged.
- title: "`keys`"
body: |
-
+
The builtin function `keys`, when given an object, returns
its keys in an array.
- title: "`has`"
body: |
-
+
The builtin function `has` returns whether the input object
has the given key, or the input array has an element at the
given index.
- title: "`del`"
body: |
-
+
The builtin function `del` removes a key and its corresponding
value from an object.
-
+
examples:
- program: 'del(.foo)'
input: '[{"foo": 42, "bar": 9001, "baz": 42}]'
- title: "`to_entries`, `from_entries`, `with_entries`"
body: |
-
+
These functions convert between an object and an array of
key-value pairs. If `to_entries` is passed an object, then
for each `k: v` entry in the input, the output array
- includes `{"key": k, "value": v}`.
+ includes `{"key": k, "value": v}`.
`from_entries` does the opposite conversion, and
`with_entries(foo)` is a shorthand for `to_entries |
- title: "`select`"
body: |
-
+
The function `select(foo)` produces its input unchanged if
`foo` returns true for that input, and produces no output
otherwise.
- title: "`arrays`, `objects`, `iterables`, `booleans`, `numbers`, `strings`, `nulls`, `values`, `scalars`"
body: |
-
+
These built-ins select only inputs that are arrays, objects,
iterables (arrays or objects), booleans, numbers, strings,
null, non-null values, and non-iterables, respectively.
- title: "`empty`"
body: |
-
+
`empty` returns no results. None at all. Not even `null`.
It's useful on occasion. You'll know if you need it :)
Outputs the paths to all the elements in its input (except it
does not output the empty list, representing . itself).
- `paths` is equivalent to
+ `paths` is equivalent to
def paths: path(recurse(if (type|. == "array" or . == "object") then .[] else empty end))|select(length > 0);
- title: "`any`"
body: |
-
+
The filter `any` takes as input an array of boolean values,
and produces `true` as output if any of the the elements of
the array is `true`.
-
+
If the input is an empty array, `any` returns `false`.
-
+
examples:
- program: any
input: '[true, false]'
- title: "`all`"
body: |
-
+
The filter `all` takes as input an array of boolean values,
and produces `true` as output if all of the the elements of
the array are `true`.
-
+
If the input is an empty array, `all` returns `true`.
-
+
examples:
- program: all
input: '[true, false]'
- title: "`range`"
body: |
-
+
The `range` function produces a range of numbers. `range(4;10)`
produces 6 numbers, from 4 (inclusive) to 10 (exclusive). The numbers
- are produced as separate outputs. Use `[range(4;10)]` to get a range as
+ are produced as separate outputs. Use `[range(4;10)]` to get a range as
an array.
examples:
- program: '[range(2;4)]'
input: 'null'
output: ['[2,3]']
-
+
- title: "`floor`"
body: |
- program: 'floor'
input: '3.14159'
output: ['3']
-
+
- title: "`sqrt`"
body: |
- program: 'sqrt'
input: '9'
output: ['3']
-
+
- title: "`tonumber`"
body: |
- title: "`type`"
body: |
-
+
The `type` function returns the type of its argument as a
string, which is one of null, boolean, number, string, array
or object.
- title: "`sort, sort_by`"
body: |
-
+
The `sort` functions sorts its input, which must be an
array. Values are sorted in the following order:
- title: "`group_by`"
body: |
-
+
`group_by(.foo)` takes as input an array, groups the
elements having the same `.foo` field into separate arrays,
and produces all of these arrays as elements of a larger
- title: "`min`, `max`, `min_by`, `max_by`"
body: |
-
+
Find the minimum or maximum element of the input array. The
`_by` versions allow you to specify a particular field or
property to examine, e.g. `min_by(.foo)` finds the object
- title: "`unique`"
body: |
-
+
The `unique` function takes as input an array and produces
an array of the same elements, in sorted order, with
duplicates removed.
- title: "`unique_by`"
body: |
-
+
The `unique_by(.foo)` function takes as input an array and produces
an array of the same elements, in sorted order, with
elqements with a duplicate `.foo` field removed. Think of it as making
an array by taking one element out of every group produced by
`group_by`.
-
+
examples:
- program: 'unique_by(.foo)'
input: '[{"foo": 1, "bar": 2}, {"foo": 1, "bar": 3}, {"foo": 4, "bar": 5}]'
- title: "`reverse`"
body: |
-
+
This function reverses an array.
examples:
- title: "`join`"
body: |
-
+
Joins the array of elements given as input, using the
argument as separator. It is the inverse of `split`: that is,
running `split("foo") | join("foo")` over any input string
returns said input string.
-
+
examples:
- program: 'join(", ")'
input: '["a","b,c,d","e"]'
- title: "`recurse`"
body: |
-
+
The `recurse` function allows you to search through a
recursive structure, and extract interesting data from all
levels. Suppose your input represents a filesystem:
{"name": "/home", "children": [
{"name": "/home/stephen", "children": [
{"name": "/home/stephen/jq", "children": []}]}]}]}
-
+
Now suppose you want to extract all of the filenames
present. You need to retrieve `.name`, `.children[].name`,
`.children[].children[].name`, and so on. You can do this
examples:
- program: 'recurse(.foo[])'
input: '{"foo":[{"foo": []}, {"foo":[{"foo":[]}]}]}'
- output:
+ output:
- '{"foo":[{"foo":[]},{"foo":[{"foo":[]}]}]}'
- '{"foo":[]}'
- '{"foo":[{"foo":[]}]}'
- title: "`recurse_down`"
body: |
-
+
A quieter version of `recurse(.[])`, equivalent to:
def recurse_down: recurse(.[]?);
- title: "`..`"
body: |
-
+
Short-hand for `recurse_down`. This is intended to resemble
the XPath `//` operator. Note that `..a` does not work; use
`..|a` instead.
- program: '"The input was \(.), which is one less than \(.+1)"'
input: '42'
output: ['"The input was 42, which is one less than 43"']
-
+
- title: "Convert to/from JSON"
body: |
characters to a `%xx` sequence.
* `@csv`:
-
+
The input must be an array, and it is rendered as CSV
with double quotes for strings, and quotes escaped by
repetition.
* `@sh`:
-
+
The input is escaped suitable for use in a command-line
for a POSIX shell. If the input is an array, the output
will be a series of space-separated strings.
-
+
* `@base64`:
The input is converted to base64 as specified by RFC 4648.
- program: '@sh "echo \(.)"'
input: "\"O'Hara's Ale\""
output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]
-
+
- title: Conditionals and Comparisons
entries:
- title: "`==`, `!=`"
to numbers. If you're coming from Javascript, jq's == is like
Javascript's === - considering values equal only when they have the
same type as well as the same value.
-
+
!= is "not equal", and 'a != b' returns the opposite value of 'a == b'
examples:
`if A then B else C end` will act the same as `B` if `A`
produces a value other than false or null, but act the same
as `C` otherwise.
-
+
Checking for false or null is a simpler notion of
"truthiness" than is found in Javascript or Python, but it
means that you'll sometimes have to be more explicit about
string is empty using `if .name then A else B end`, you'll
need something more like `if (.name | length) > 0 then A else
B end` instead.
-
+
If the condition A produces multiple results, it is
considered "true" if any of those results is not false or
null. If it produces zero results, it's considered false.
-
+
More cases can be added to an if using `elif A then B` syntax.
examples:
end
input: 2
output: ['"many"']
-
+
- title: "`>, >=, <=, <`"
body: |
-
+
The comparison operators `>`, `>=`, `<=`, `<` return whether
their left argument is greater than, greater than or equal
to, less than or equal to or less than their right argument
so it is called as a filter to which things can be piped
rather than with special syntax, as in `.foo and .bar |
not`.
-
+
These three only produce the values "true" and "false", and
so are only useful for genuine Boolean operations, rather
than the common Perl/Python/Ruby idiom of
"value_that_may_be_null or default". If you want to use this
form of "or", picking between two values rather than
evaluating a condition, see the "//" operator below.
-
+
examples:
- program: '42 and "a string"'
input: 'null'
- program: '.foo // 42'
input: '{}'
output: [42]
-
+
- title: Advanced features
body: |
Variables are an absolute necessity in most programming languages, but
entries:
- title: Variables
body: |
-
+
In jq, all filters have an input and an output, so manual
plumbing is not necessary to pass a value from one part of a program
to the next. Many expressions, for instance `a + b`, pass their input
to two distinct subexpressions (here `a` and `b` are both passed the
same input), so variables aren't usually necessary in order to use a
value twice.
-
+
For instance, calculating the average value of an array of numbers
requires a few variables in most languages - at least one to hold the
array, perhaps one for each element or for a loop counter. In jq, it's
simply `add / length` - the `add` expression is given the array and
produces its sum, and the `length` expression is given the array and
produces its length.
-
+
So, there's generally a cleaner way to solve most problems in jq than
defining variables. Still, sometimes they do make things easier, so jq
lets you define variables using `expression as $variable`. All
variable names start with `$`. Here's a slightly uglier version of the
array-averaging example:
-
+
length as $array_length | add / $array_length
-
+
We'll need a more complicated problem to find a situation where using
variables actually makes our lives easier.
-
-
+
+
Suppose we have an array of blog posts, with "author" and "title"
fields, and another object which is used to map author usernames to
real names. Our input looks like:
-
+
{"posts": [{"title": "Frist psot", "author": "anon"},
{"title": "A well-written article", "author": "person1"}],
"realnames": {"anon": "Anonymous Coward",
"person1": "Person McPherson"}}
-
+
We want to produce the posts with the author field containing a real
name, as in:
-
+
{"title": "Frist psot", "author": "Anonymous Coward"}
{"title": "A well-written article", "author": "Person McPherson"}
-
+
We use a variable, $names, to store the realnames object, so that we
can refer to it later when looking up author usernames:
-
+
.realnames as $names | .posts[] | {title, author: $names[.author]}
-
+
The expression `exp as $x | ...` means: for each value of expression
`exp`, run the rest of the pipeline with the entire original input, and
- with `$x` set to that value. Thus `as` functions as something of a
+ with `$x` set to that value. Thus `as` functions as something of a
foreach loop.
Variables are scoped over the rest of the expression that defines
- them, so
-
+ them, so
+
.realnames as $names | (.posts[] | {title, author: $names[.author]})
-
- will work, but
-
+
+ will work, but
+
(.realnames as $names | .posts[]) | {title, author: $names[.author]}
-
+
won't.
examples:
- title: 'Defining Functions'
body: |
-
+
You can give a filter a name using "def" syntax:
def increment: . + 1;
-
+
From then on, `increment` is usable as a filter just like a
builtin function (in fact, this is how some of the builtins
are defined). A function may take arguments:
- title: Reduce
body: |
-
+
The `reduce` syntax in jq allows you to combine all of the
results of an expression by accumulating them into a single
answer. As an example, we'll pass `[3,2,1]` to this expression:
reduce .[] as $item (0; . + $item)
-
+
For each result that `.[]` produces, `. + $item` is run to
accumulate a running total, starting from 0. In this
example, `.[]` produces the results 3, 2, and 1, so the
effect is similar to running something like this:
- 0 | (3 as $item | . + $item) |
+ 0 | (3 as $item | . + $item) |
(2 as $item | . + $item) |
(1 as $item | . + $item)
- program: 'reduce .[] as $item (0; . + $item)'
input: '[10,2,5,3]'
output: ['20']
-
+
- title: Assignment
body: |
entries:
- title: "`=`"
body: |
-
+
The filter `.foo = 1` will take as input an object
and produce as output an object with the "foo" field set to
1. There is no notion of "modifying" or "changing" something
in jq - all jq values are immutable. For instance,
-
+
.foo = .bar | .foo.baz = 1
-
+
will not have the side-effect of setting .bar.baz to be set
to 1, as the similar-looking program in Javascript, Python,
Ruby or other languages would. Unlike these languages (but
"the same object". They can be equal, or not equal, but if
we change one of them in no circumstances will the other
change behind our backs.
-
+
This means that it's impossible to build circular values in
jq (such as an array whose first element is itself). This is
quite intentional, and ensures that anything a jq program
works out the new value for the property being assigned to by running
the old value through this expression. For instance, .foo |= .+1 will
build an object with the "foo" field set to the input's "foo" plus 1.
-
+
This example should show the difference between '=' and '|=':
-
+
Provide input '{"a": {"b": 10}, "b": 20}' to the programs:
-
+
.a = .b
.a |= .b
-
+
The former will set the "a" field of the input to the "b" field of the
input, and produce the output {"a": 20}. The latter will set the "a"
field of the input to the "a" field's "b" field, producing {"a": 10}.
- program: .foo += 1
input: '{"foo": 42}'
output: ['{"foo": 43}']
-
+
- title: Complex assignments
body: |
Lots more things are allowed on the left-hand side of a jq assignment
than in most langauges. We've already seen simple field accesses on
the left hand side, and it's no surprise that array accesses work just
as well:
-
+
.posts[0].title = "JQ Manual"
-
+
What may come as a surprise is that the expression on the left may
produce multiple results, referring to different points in the input
document:
-
+
.posts[].comments |= . + ["this is great"]
-
+
That example appends the string "this is great" to the "comments"
array of each post in the input (where the input is an object with a
field "posts" which is an array of posts).
-
+
When jq encounters an assignment like 'a = b', it records the "path"
taken to select a part of the input document while executing a. This
path is then used to find which part of the input to change while
executing the assignment. Any filter may be used on the
left-hand side of an equals - whichever paths it selects from the
input will be where the assignment is performed.
-
+
This is a very powerful operation. Suppose we wanted to add a comment
to blog posts, using the same "blog" input above. This time, we only
want to comment on the posts written by "stedolan". We can find those
posts using the "select" function described earlier:
-
+
.posts[] | select(.author == "stedolan")
-
+
The paths provided by this operation point to each of the posts that
"stedolan" wrote, and we can comment on each of them in the same way
that we did before:
-
+
(.posts[] | select(.author == "stedolan") | .comments) |=
. + ["terrible."]
## DESCRIPTION
`jq` can transform JSON in various ways, by selecting, iterating,
-reducing and otherwise mangling JSON documents.
+reducing and otherwise mangling JSON documents.
This version of `jq` was built without a manual, so this manpage is a
stub. For full documentation of the `jq` language, see:
http://stedolan.github.io/jq
-
+
## BUGS
Presumably. Report them or discuss them at:
</IfModule>
</IfModule>
-# Cache, aggressively
+# Cache, aggressively
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 10 days"
# Mime-types
AddType application/vnd.ms-fontobject .eot
AddType font/ttf .ttf
-AddType font/otf .otf
\ No newline at end of file
+AddType font/otf .otf
root: '/jq'
footer: |
-
+
This website is made with [Bonsai](http://www.tinytree.info) and
[Twitter Bootstrap](http://twitter.github.com/bootstrap/), themed
with [Bootswatch](http://bootswatch.com).
case 38:
YY_RULE_SETUP
#line 88 "lexer.l"
-{
- yylval->literal = jv_parse_sized(yytext, yyleng); return LITERAL;
+{
+ yylval->literal = jv_parse_sized(yytext, yyleng); return LITERAL;
}
YY_BREAK
case 39:
case IN_BRACE: match = ret = '}'; break;
case IN_QQINTERP:
- match = ')';
+ match = ')';
ret = QQSTRING_INTERP_END;
break;
yylval->literal = jv_string_sized(yytext + 1, yyleng - 1); return FORMAT;
}
-[0-9.]+([eE][+-]?[0-9]+)? {
- yylval->literal = jv_parse_sized(yytext, yyleng); return LITERAL;
+[0-9.]+([eE][+-]?[0-9]+)? {
+ yylval->literal = jv_parse_sized(yytext, yyleng); return LITERAL;
}
"\"" {
return INVALID_CHARACTER;
}
}
-
+
([a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]* { yylval->literal = jv_string(yytext); return IDENT;}
\.[a-zA-Z_][a-zA-Z_0-9]* { yylval->literal = jv_string(yytext+1); return FIELD;}
case IN_BRACE: match = ret = '}'; break;
case IN_QQINTERP:
- match = ')';
+ match = ')';
ret = QQSTRING_INTERP_END;
break;
#!/bin/sh
-# This script is used to cross-compile binaries for
+# This script is used to cross-compile binaries for
# platforms other than the current one
# Usage: $0 <platformname> <configure options>
first = True
for i in range(0,len(t),16):
print ((" {" if i == 0 else " ") +
- ", ".join("0x%02x"%n for n in t[i:i+16]) +
+ ", ".join("0x%02x"%n for n in t[i:i+16]) +
("," if i + 16 < len(t) else "};"))
if 0xE0 <= c <= 0xEF: return 3, mask(4)
if 0xF0 <= c <= 0xF4: return 4, mask(3)
if 0xF4 <= c <= 0xFF: return 0, 0
-
+
table = lambda i: [utf8info(c)[i] for c in range(256)]
print("#define UTF8_CONTINUATION_BYTE ((unsigned char)255)")