Final touch.

This commit is contained in:
Jack Kuan 2022-06-05 18:48:08 -04:00
parent 0ef85542ef
commit 0801451fd7

View File

@ -8,12 +8,15 @@ translators:
--- ---
`jq` is a tool for transforming JSON inputs and generating JSON outputs. As a `jq` is a tool for transforming JSON inputs and generating JSON outputs. As a
programming language,`jq` supports boolean and arithmetic expressions, object programming language, jq supports boolean and arithmetic expressions, object
and array indexing; it has conditionals, functions, and even exception and array indexing; it has conditionals, functions, and even exception
handling... etc. Knowing `jq` enables you to easily write small programs that handling... etc. Knowing jq enables you to easily write small programs that
can perform complex queries on JSON documents to find answers, make reports, or can perform complex queries on JSON documents to find answers, make reports, or
to produce another JSON document for further processing by other programs. to produce another JSON document for further processing by other programs.
> **NOTE**: This guide demonstrates the use of jq from the command line,
> specifically, under an environment running the Bash shell.
```bash ```bash
# When running jq from the command line, jq program code can be specified as the # When running jq from the command line, jq program code can be specified as the
# first argument after any options to `jq`. We often quote such jq program with # first argument after any options to `jq`. We often quote such jq program with
@ -251,7 +254,7 @@ jq -n '[2*3, 8-1, 16/2], {("tw" + "o"): (1 + 1)}'
jq -n '{ key_1: "value1" }' jq -n '{ key_1: "value1" }'
# If a JSON object's key's value is ommited, it is looked up in the current # If a JSON object's key's value is ommited, it is looked up in the current
# input using the key: # input using the key: (see next example for the meaning of `... | ...`)
# #
jq -n '{c: 3} | {a: 1, "b", c}' jq -n '{c: 3} | {a: 1, "b", c}'
@ -321,7 +324,7 @@ jq -n '1, 2, 3 | ., 4 | .'
# Below are some examples of array index and object attribute lookups using # Below are some examples of array index and object attribute lookups using
# the '[expr]` operator after an expression. If `expr` is a number then it's # the `[expr]` operator after an expression. If `expr` is a number then it's
# an array index lookup; otherwise, it should be a string, in which case it's # an array index lookup; otherwise, it should be a string, in which case it's
# an object attribute lookup: # an object attribute lookup:
@ -483,6 +486,12 @@ jq -n '1, 2, 3, 4, 5 | select(. % 2 != 0)' # NOTE: % gives the remainder.
# treated as a lambda expression with the calling context of the call # treated as a lambda expression with the calling context of the call
# site as its scope for variable and function references used in the # site as its scope for variable and function references used in the
# expression. # expression.
#
# In the above example, the expression `. % 2 != 0` is what's passed to
# `select/1` as the argument, not `true` or `false`, which is what would
# have been the case had the (boolean) expression was evaluated before it's
# passed to the function.
# The `range/1`, `range/2`, and `range/3` built-in functions generate # The `range/1`, `range/2`, and `range/3` built-in functions generate
# integers within a given range. # integers within a given range.
@ -694,7 +703,7 @@ jq -n '["a", "b", "c"] | reduce .[] as $i (""; . + $i)' # => "abc"
# #
# reduce (1, 2, 3, 4, 5) as $i (0; . + $i) # reduce (1, 2, 3, 4, 5) as $i (0; . + $i)
# #
# can be think of as doing: # can be thought of as doing:
# #
# 0 + 1 | . + 2 | . + 3 | . + 4 | . + 5 # 0 + 1 | . + 2 | . + 3 | . + 4 | . + 5
# #
@ -781,6 +790,30 @@ EOF
# c's total is 12 # c's total is 12
# jq supports destructing during varible binding. This lets you extract values
# from an array or an object and bind them to variables.
#
jq -n '[range(5)] | . as [$first, $second] | $second'
# Output:
# 1
jq -n '{ name: "Tom", numbers: [1, 2, 3], age: 32}
| . as {
name: $who, # bind .name to $who
$name, # shorthand for `name: $name`
numbers: [$first, $second],
}
| $name, $second, $first, $who
'
# Output:
# "Tom"
# 2
# 1
# "Tom"
# In jq, values can be assigned to an array index or object key via the # In jq, values can be assigned to an array index or object key via the
# assignment operator, `=`. The same current input is given to both sides # assignment operator, `=`. The same current input is given to both sides
# of the assignment operator, and the assignment itself evaluates to the # of the assignment operator, and the assignment itself evaluates to the
@ -865,11 +898,12 @@ jq -n '
# - Function parameters are separated by `;` (semicolor). This is consistent with # - Function parameters are separated by `;` (semicolor). This is consistent with
# passing multiple arguments when calling a function. # passing multiple arguments when calling a function.
# #
# - `def f($a; $b): ...;` is a shorthand for: `def f(a; b): a as $a | b as $b | ...` # - A function can call itself; in fact, jq has TCO (Tail Call Optimization).
# #
# - `def f($a; $b): ...;` is a shorthand for: `def f(a; b): a as $a | b as $b | ...`
``` ```
## Further Reading ## Further Reading
- https://stedolan.github.io/jq/manual/ - https://stedolan.github.io/jq/manual/
- https://github.com/stedolan/jq/wiki/jq-Language-Description - https://github.com/stedolan/jq/wiki/jq-Language-Description