summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--AUTHORS2
-rw-r--r--COPYING4
-rw-r--r--Makefile.am8
-rw-r--r--configure.ac2
-rw-r--r--docs/Rakefile16
-rw-r--r--docs/content/2.download/default.yml4
-rw-r--r--docs/content/3.manual/manual.yml24
-rw-r--r--docs/content/3.manual/v1.3/manual.yml252
-rw-r--r--docs/content/3.manual/v1.4/manual.yml296
-rw-r--r--docs/default_manpage.md4
-rw-r--r--docs/public/.htaccess4
-rw-r--r--docs/site.yml2
-rw-r--r--lexer.c6
-rw-r--r--lexer.l8
-rwxr-xr-xscripts/crosscompile2
-rw-r--r--scripts/gen_utf8_tables.py4
16 files changed, 319 insertions, 319 deletions
diff --git a/AUTHORS b/AUTHORS
index dda9b014..dcaf7b4e 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -18,7 +18,7 @@ Filippo Valsorda <filippo.valsorda@gmail.com> - recursive object merge (
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
diff --git a/COPYING b/COPYING
index 03b0f56f..6026d7fa 100644
--- a/COPYING
+++ b/COPYING
@@ -30,9 +30,9 @@ license, which can be found at:
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:
diff --git a/Makefile.am b/Makefile.am
index c3a771fd..6acb7200 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -26,7 +26,7 @@ lexer.c: lexer.l
$(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:
@@ -146,7 +146,7 @@ DOC_FILES = docs/content docs/public docs/templates docs/site.yml \
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 \
@@ -164,7 +164,7 @@ EXTRA_DIST = $(man_MANS) $(TESTS) $(TEST_LOG_COMPILER) jq.spec \
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
@@ -187,7 +187,7 @@ dist-clean-local:
clean-local-docs:
if ENABLE_DOCS
rm -f jq.1
-endif
+endif
clean-local-gcov:
rm -f *.gcno *.gcda *.gcov
diff --git a/configure.ac b/configure.ac
index d9057e39..122887a6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -200,7 +200,7 @@ AC_LINK_IFELSE([AC_LANG_SOURCE([
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)
diff --git a/docs/Rakefile b/docs/Rakefile
index 167ee87d..8fba5a96 100644
--- a/docs/Rakefile
+++ b/docs/Rakefile
@@ -19,7 +19,7 @@ module ExtraFilters
def json(input)
input.to_json
end
-
+
def unique(input)
@n = (@n || 0) + 1
input + @n.to_s
@@ -32,15 +32,15 @@ Liquid::Template.register_filter(ExtraFilters)
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 => ["/"]
@@ -50,7 +50,7 @@ task :serve do
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)
@@ -96,7 +96,7 @@ task :manpage do
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 ""
@@ -116,7 +116,7 @@ task :manpage do
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
diff --git a/docs/content/2.download/default.yml b/docs/content/2.download/default.yml
index e96986dd..a88ced03 100644
--- a/docs/content/2.download/default.yml
+++ b/docs/content/2.download/default.yml
@@ -15,7 +15,7 @@ body:
### 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`.
@@ -126,7 +126,7 @@ body:
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
diff --git a/docs/content/3.manual/manual.yml b/docs/content/3.manual/manual.yml
index 8ac564c4..67ef47ff 100644
--- a/docs/content/3.manual/manual.yml
+++ b/docs/content/3.manual/manual.yml
@@ -619,7 +619,7 @@ sections:
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
@@ -710,12 +710,12 @@ sections:
- 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"]'
@@ -723,7 +723,7 @@ sections:
- program: 'map(in([0,1]))'
input: '[2, 0]'
output: ['[false, true]']
-
+
- title: "`path(path_expression)`"
body: |
@@ -868,10 +868,10 @@ sections:
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`.
@@ -879,7 +879,7 @@ sections:
- 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}']
@@ -1413,7 +1413,7 @@ sections:
- 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:
@@ -1576,7 +1576,7 @@ sections:
- 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:
@@ -2589,7 +2589,7 @@ sections:
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
@@ -2842,7 +2842,7 @@ sections:
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.
@@ -2875,7 +2875,7 @@ sections:
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.
diff --git a/docs/content/3.manual/v1.3/manual.yml b/docs/content/3.manual/v1.3/manual.yml
index 0f481451..a6cc436f 100644
--- a/docs/content/3.manual/v1.3/manual.yml
+++ b/docs/content/3.manual/v1.3/manual.yml
@@ -16,7 +16,7 @@ body: |
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
@@ -50,7 +50,7 @@ manpage_intro: |
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
@@ -68,11 +68,11 @@ manpage_epilogue: |
## 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
@@ -83,46 +83,46 @@ sections:
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
@@ -138,7 +138,7 @@ sections:
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.
@@ -154,7 +154,7 @@ sections:
- 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.
@@ -169,7 +169,7 @@ sections:
- 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
@@ -195,7 +195,7 @@ sections:
- program: '.[2:4]'
input: '["a","b","c","d","e"]'
output: ['["c", "d"]']
-
+
- program: '.[:3]'
input: '["a","b","c","d","e"]'
output: ['["a", "b", "c"]']
@@ -206,7 +206,7 @@ sections:
- 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
@@ -233,7 +233,7 @@ sections:
- 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
@@ -248,18 +248,18 @@ sections:
- 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
@@ -273,7 +273,7 @@ sections:
- 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".
@@ -287,7 +287,7 @@ sections:
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
@@ -295,7 +295,7 @@ sections:
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
@@ -315,49 +315,49 @@ sections:
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"]}'
@@ -376,7 +376,7 @@ sections:
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: |
@@ -390,12 +390,12 @@ sections:
- **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.
@@ -464,7 +464,7 @@ sections:
- title: `keys`
body: |
-
+
The builtin function `keys`, when given an object, returns
its keys in an array.
@@ -487,7 +487,7 @@ sections:
- 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.
@@ -506,11 +506,11 @@ sections:
- 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 |
@@ -531,7 +531,7 @@ sections:
- title: `select`
body: |
-
+
The function `select(foo)` produces its input unchanged if
`foo` returns true for that input, and produces no output
otherwise.
@@ -546,7 +546,7 @@ sections:
- 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 :)
@@ -598,10 +598,10 @@ sections:
- 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:
@@ -611,7 +611,7 @@ sections:
- program: '[range(2;4)]'
input: 'null'
output: ['[2,3]']
-
+
- title: `tonumber`
body: |
@@ -638,7 +638,7 @@ sections:
- 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.
@@ -650,7 +650,7 @@ sections:
- title: `sort, sort_by`
body: |
-
+
The `sort` functions sorts its input, which must be an
array. Values are sorted in the following order:
@@ -682,7 +682,7 @@ sections:
- 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
@@ -699,7 +699,7 @@ sections:
- 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
@@ -715,7 +715,7 @@ sections:
- 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.
@@ -727,7 +727,7 @@ sections:
- title: `reverse`
body: |
-
+
This function reverses an array.
examples:
@@ -766,7 +766,7 @@ sections:
- 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:
@@ -778,7 +778,7 @@ sections:
{"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
@@ -789,7 +789,7 @@ sections:
examples:
- program: 'recurse(.foo[])'
input: '{"foo":[{"foo": []}, {"foo":[{"foo":[]}]}]}'
- output:
+ output:
- '{"foo":[{"foo":[]},{"foo":[{"foo":[]}]}]}'
- '{"foo":[]}'
- '{"foo":[{"foo":[]}]}'
@@ -807,7 +807,7 @@ sections:
- 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: |
@@ -836,17 +836,17 @@ sections:
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.
@@ -879,7 +879,7 @@ sections:
- program: '@sh "echo \(.)"'
input: "\"O'Hara's Ale\""
output: ["\"echo 'O'\\\\''Hara'\\\\''s Ale'\""]
-
+
- title: Conditionals and Comparisons
entries:
- title: `==`, `!=`
@@ -891,7 +891,7 @@ sections:
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:
@@ -904,7 +904,7 @@ sections:
`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
@@ -912,11 +912,11 @@ sections:
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:
@@ -930,10 +930,10 @@ sections:
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
@@ -960,14 +960,14 @@ sections:
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'
@@ -1005,7 +1005,7 @@ sections:
- program: '.foo // 42'
input: '{}'
output: [42]
-
+
- title: Advanced features
body: |
Variables are an absolute necessity in most programming languages, but
@@ -1030,67 +1030,67 @@ sections:
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:
@@ -1100,11 +1100,11 @@ sections:
- 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:
@@ -1135,19 +1135,19 @@ sections:
- 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 | . +