### Serve Documentation Website Source: https://github.com/jqlang/jq/wiki/Docs-and-tests Starts a local web server to display the documentation website. Requires a working Ruby setup. ```bash rake serve ``` -------------------------------- ### Initialize and Install jq with Flox Source: https://github.com/jqlang/jq/wiki/Installation Demonstrates initializing a new environment with Flox and then installing jq into it. This is for managing development environments. ```console $ flox init ✨ Created environment 'tmp.kAmqr3RpBT' (aarch64-darwin) Next: $ flox search <- Search for a package $ flox install <- Install a package into an environment $ flox activate <- Enter the environment $ flox edit <- Add environment variables and shell hooks $ flox install jq ✅ 'jq' installed to environment 'tmp.bCViSurqsu' $ flox activate ✅ You are now using the environment 'tmp.bCViSurqsu'. To stop using this environment, type 'exit' flox [tmp.bCViSurqsu] $ jq --version jq-1.7.1 ``` -------------------------------- ### Install 0install script Source: https://github.com/jqlang/jq/wiki/Installation Downloads and makes executable the 0install installation script. Follow the script's directions for completion. ```bash curl -O https://get.0install.net/0install.sh && chmod +x 0install.sh ./0install.sh ``` -------------------------------- ### Install jq with Homebrew Source: https://github.com/jqlang/jq/wiki/Installation Installs the stable version of jq using the Homebrew package manager. Verify Homebrew is installed first. ```bash $ brew install jq ``` -------------------------------- ### Add jq using 0install Source: https://github.com/jqlang/jq/wiki/Installation Installs the most recent version of jq known to 0install. Ensure 0install is installed and ~/bin/jq does not exist. ```bash 0install add jq https://apps.0install.net/utils/jq.xml ``` -------------------------------- ### Install jq using Chocolatey on Windows Source: https://github.com/jqlang/jq/wiki/Installation Installs a pre-compiled jq binary on Windows using the Chocolatey package manager. ```bash choco install jq ``` -------------------------------- ### Install Bison 3 from Source Source: https://github.com/jqlang/jq/wiki/Installation Installs Bison 3 from source, a dependency for building jq. Ensure you have wget and tar installed. ```bash cd /tmp wget http://ftp.gnu.org/gnu/bison/bison-3.0.3.tar.gz tar -xvzf bison-3.0.3.tar.gz cd bison-3.0.3 sudo port install m4 ./configure --prefix=/usr/local/bison sudo make install sudo ln -s /usr/local/bison/bin/bison /usr/bin/bison ``` -------------------------------- ### Example Manual Entry Structure Source: https://github.com/jqlang/jq/wiki/Docs-and-tests Illustrates the structure of an example within the manual's YAML file. 'program', 'input', and 'output' are YAML strings and lists of strings respectively. ```yaml examples: - program: '.foo, .bar' input: '{"foo": 42, "bar": "something else", "baz": true}' output: ['42', '"something else"'] ``` -------------------------------- ### Install ruby-jq for Ruby Source: https://github.com/jqlang/jq/wiki/FAQ Install the ruby-jq gem for Ruby development. Refer to the GitHub repository for more details. ```bash gem install ruby-jq # For details, see https://github.com/winebarrel/ruby-jq ``` -------------------------------- ### Install jq using Homebrew on Linux/macOS Source: https://github.com/jqlang/jq/wiki/Installation Installs jq using Homebrew. This command is run within a bash window after installing bash for Windows 10 and Homebrew for Linux. ```bash brew install jq ``` -------------------------------- ### Add a specific jq version using 0install Source: https://github.com/jqlang/jq/wiki/Installation Installs a particular version of jq, e.g., 1.4, using 0install. Specify the version and a name for the installation. ```bash 0install add --version=1.4 jq1.4 https://apps.0install.net/utils/jq.xml ``` -------------------------------- ### Sync Dependencies with pipenv Source: https://github.com/jqlang/jq/blob/master/docs/README.md After installing pipenv, navigate to the `docs/` directory and run this command to install project dependencies. This ensures you have all necessary tools for documentation generation. ```bash pipenv sync ``` -------------------------------- ### Build Documentation Website Source: https://github.com/jqlang/jq/wiki/Docs-and-tests Generates the complete documentation website in the 'output' folder. Requires a working Ruby setup. ```bash rake build ``` -------------------------------- ### Install jq for Python Source: https://github.com/jqlang/jq/wiki/FAQ Install the jq library for Python using pip. Multiple jq Python wrappers are available. ```bash pip install jq # For details, see https://pypi.python.org/pypi/jq ``` ```bash pip install pyjq # For details, see https://pypi.python.org/pypi/pyjq ``` ```bash pip install jqpy # For details, see https://baterflyrity.github.io/jqpy/ ``` -------------------------------- ### Install jq with MacPorts Source: https://github.com/jqlang/jq/wiki/Installation Installs jq version 1.5 using the MacPorts package manager. Ensure MacPorts is installed before running this command. ```bash sudo port install jq ``` -------------------------------- ### Build jq from Source Source: https://github.com/jqlang/jq/blob/master/README.md Instructions for building jq from source, including submodule updates, configuration, and compilation. Ensure all dependencies are installed. ```console git submodule update --init # if building from git to get oniguruma autoreconf -i # if building from git ./configure --with-oniguruma=builtin make clean # if upgrading from a version previously built from source make -j8 make check sudo make install ``` -------------------------------- ### jq foreach loop with state variable example Source: https://github.com/jqlang/jq/blob/master/NEWS.md Demonstrates the `foreach` construct in jq. The first example shows a basic usage, and the second illustrates how the state variable is correctly maintained across iterations, fixing a previous issue where it was reset. ```sh $ jq -n '[1, 2] | foreach .[] as $x (0, 1; . + $x)' ``` ```sh $ jq -n 'reduce range(5) as $x (0; .+$x | select($x!=2))' ``` ```sh $ jq -nc '[foreach range(5) as $x (0; .+$x | select($x!=2); [$x,.])]' ``` -------------------------------- ### Install latest jq from Homebrew HEAD Source: https://github.com/jqlang/jq/wiki/Installation Installs the most recent development version of jq from the Homebrew HEAD. Use with caution as it may be unstable. ```bash brew install --HEAD jq ``` -------------------------------- ### Install RVM and Ruby for Docs Source: https://github.com/jqlang/jq/wiki/Installation Installs RVM and a specific Ruby version, which was previously a dependency for building jq documentation. Note that python is now used for building docs. ```bash cd ~/jq/docs curl -L https://get.rvm.io | bash -s stable --ruby=1.9.3 cd docs bundle install ``` -------------------------------- ### Install Oniguruma Dependency Source: https://github.com/jqlang/jq/wiki/Installation Installs the Oniguruma regex library from source, a dependency for jq. This is a manual compilation process. ```bash wget https://web.archive.org/web/20150803013327/http://www.geocities.jp/kosako3/oniguruma/archive/onig-5.9.6.tar.gz tar -xf onig-5.9.6.tar.gz cd onig-5.9.6 ./configure make sudo make install ``` -------------------------------- ### Rename keys using translate_keys (Example 1) Source: https://github.com/jqlang/jq/wiki/FAQ Example demonstrating how to prepend a string to all keys in a JSON entity using the `translate_keys` function. ```jq {"a": 1, "b": [{"c":2}] } | translate_keys( "@" + . ) ``` -------------------------------- ### Install jq Release Binary with Dockerfile Source: https://github.com/jqlang/jq/wiki/Installation Installs a specific version of the jq release binary within a Docker container. It downloads the binary, verifies its signature, and makes it executable. ```dockerfile ENV JQ_VERSION='1.5' RUN wget --no-check-certificate https://raw.githubusercontent.com/jqlang/jq/master/sig/jq-release.key -O /tmp/jq-release.key && \ wget --no-check-certificate https://raw.githubusercontent.com/jqlang/jq/master/sig/v${JQ_VERSION}/jq-linux64.asc -O /tmp/jq-linux64.asc && \ wget --no-check-certificate https://github.com/jqlang/jq/releases/download/jq-${JQ_VERSION}/jq-linux64 -O /tmp/jq-linux64 && \ gpg --import /tmp/jq-release.key && \ gpg --verify /tmp/jq-linux64.asc /tmp/jq-linux64 && \ cp /tmp/jq-linux64 /usr/bin/jq && \ chmod +x /usr/bin/jq && \ rm -f /tmp/jq-release.key && \ rm -f /tmp/jq-linux64.asc && \ rm -f /tmp/jq-linux64 ``` -------------------------------- ### Run decNumber Example 1 Source: https://github.com/jqlang/jq/blob/master/vendor/decNumber/readme.txt Execute the compiled example1 program with two numeric arguments to observe their addition. ```bash example1 1.23 1.27 ``` -------------------------------- ### jq zip function and testzip usage Source: https://github.com/jqlang/jq/wiki/X---Experimental-Benchmarks Defines a 'zip' function to combine headers with data and a 'testzip' function to create a large dataset for testing. The example shows how to use 'testzip' to generate a dataset of a specified size and then get its length. ```jq def zip(headers): . headers as $headers | [$headers, .] | transpose | map({(.[0]): .[1]}) | add ; def testzip(n): [range(0;n)] as $row | $row | zip( $row|map(tostring) ) ; testzip(1000000) | length ``` -------------------------------- ### Install pipenv Source: https://github.com/jqlang/jq/blob/master/docs/README.md Use this command to install pipenv, a dependency manager for Python projects. You may need to use `pip3` depending on your system configuration. ```bash pip install pipenv ``` -------------------------------- ### decNumber Example 1 Output Source: https://github.com/jqlang/jq/blob/master/vendor/decNumber/readme.txt The expected output when running example1 with '1.23' and '1.27' as arguments, demonstrating decimal addition. ```text 1.23 + 1.27 => 2.50 ``` -------------------------------- ### Compile decNumber Example 1 Source: https://github.com/jqlang/jq/blob/master/vendor/decNumber/readme.txt Compile example1.c along with decNumber.c and decContext.c. Compiler optimization is recommended. Ensure stdint.h is available or consult documentation for alternatives. ```bash gcc -o example1 example1.c decNumber.c decContext.c ``` -------------------------------- ### Install jsonic for non-strict JSON conversion Source: https://github.com/jqlang/jq/wiki/FAQ Install the jsonic package via npm to convert non-strict JSON formats to standard JSON. ```bash npm install jsonic ``` -------------------------------- ### jq binding syntax precedence change example Source: https://github.com/jqlang/jq/blob/master/NEWS.md Demonstrates a breaking change in jq's binding syntax precedence. The first example shows how `as $x` now binds correctly before unary operators, and the second shows binding before binary operators. ```sh $ jq -nc '[-1 as $x | 1,$x]' ``` ```sh $ jq -nc '1 | . + 2 as $x | -$x' ``` -------------------------------- ### Run All Tests Source: https://github.com/jqlang/jq/wiki/Docs-and-tests Executes all tests, including manual examples, jq-coded tests, and shell-coded tests. Recommended to run under Valgrind for memory leak detection. ```bash make check ``` -------------------------------- ### Generate Manual Page Source: https://github.com/jqlang/jq/wiki/Docs-and-tests Generates the roff manual page for jq. Requires a working Ruby setup. ```bash make jq.1 ``` -------------------------------- ### Example: C-coded Built-in Calling jq Wrapper Source: https://github.com/jqlang/jq/wiki/How-To:-Add-built-in-functions When a C-coded builtin needs to interact with jq's execution context (e.g., call other jq functions), it should be wrapped by a jq-coded function. This example shows the concept, where `_sort_by_impl` is a C implementation called by a jq wrapper. ```jq # jq wrapper for a C implementation def sort_by(f): _sort_by_impl(f); ``` -------------------------------- ### Cartesian Product Example Source: https://github.com/jqlang/jq/wiki/How-to:-Avoid-Pitfalls Demonstrates how jq generates Cartesian products when multiple values are piped or combined. Each element from the first set is combined with each element from the second. ```jq 3 4 3 4 ``` ```jq $ jq -n '(1,2) as $i | (3,4) | "\($i),\(.)"' "1,3" "1,4" "2,3" "2,4" ``` -------------------------------- ### Named Group Backreference Example Source: https://github.com/jqlang/jq/wiki/Docs-for-Oniguruma-Regular-Expressions-(RE.txt) Demonstrates backreferencing using named groups with relative level adjustments. This example shows how to reference groups based on their nesting depth. ```Ruby r = Regexp.compile(<<'__REGEXP__'.strip, Regexp::EXTENDED) (? \g \g* \g ){0} (? < \g \s* > ){0} (? [a-zA-Z_:]+ ){0} (? [^<&]+ (?:\g | [^<&]+)* ){0} (? >){0} \g __REGEXP__ p r.match('fbbbf').captures ``` -------------------------------- ### Check jq executable path Source: https://github.com/jqlang/jq/wiki/FAQ If jq is installed but not running, try executing it using its full path. Ensure the binary file has execute permissions on Unix-like systems. ```bash jq --version ``` -------------------------------- ### Cross-compile Oniguruma for Windows 64-bit Source: https://github.com/jqlang/jq/wiki/Cross-compilation Builds the Oniguruma library for a 64-bit Windows target, installing it into a specified prefix. ```shell : ; mkdir -p /home/build/proto/win64 : ; PATH=/home/build/mingw/mingw-w64-x86_64/bin:$PATH : ; cd /home/build/ws/onig-5.9.5 : ; ./configure --target=win64-x86_64 --host=x86_64-w64-mingw32 --prefix=/home/build/proto/win64/usr/local : ; make -j16 LDFLAGS=-all-static clean all install ``` -------------------------------- ### Example Usage of Arithmetic Expression Evaluator Source: https://github.com/jqlang/jq/wiki/Parsing-Expression-Grammars Demonstrates how to use the 'evaluate' jq filter with a sample arithmetic expression string. ```bash $ jq -f arithmetic.jq <<< "(1+2)*(3+4)+(1*1)" 22 ``` -------------------------------- ### jq Closure Example Source: https://github.com/jqlang/jq/wiki/Advanced-Topics Illustrates how functions in jq are closures. This example shows a local variable '$a' captured by the argument closure 'x', demonstrating that the binding within 'foo' is not visible to 'x'. ```jq def foo(x): .a as $a | x.b * 2; {"a":{"a":3, "b":4}} | .a as $a | foo($a) ``` -------------------------------- ### Cross-compile Oniguruma for Windows 32-bit Source: https://github.com/jqlang/jq/wiki/Cross-compilation Builds the Oniguruma library for a 32-bit Windows target, installing it into a specified prefix. ```shell : ; mkdir -p /home/build/proto/win32 : ; PATH=/home/build/mingw/mingw-w32-x86_64/bin:$PATH : ; cd /home/build/ws/onig-5.9.5 : ; ./configure --target=win32-i86 --host=i686-w64-mingw32 --prefix=/home/build/proto/win32/usr/local : ; make -j16 LDFLAGS=-all-static clean all install ``` -------------------------------- ### Display Manual Page Source: https://github.com/jqlang/jq/wiki/Docs-and-tests Displays the generated roff manual page in a pager, similar to 'man jq'. Requires a working Ruby setup. ```bash nroff -man jq.1 | $PAGER ``` -------------------------------- ### Build jq from source with alternative oniguruma Source: https://github.com/jqlang/jq/wiki/Installation Builds jq from source, allowing the use of an alternative oniguruma regex library. Requires autoconf and libtool. Ensure dependencies are installed. ```bash autoreconf -i ./configure --disable-maintainer-mode make ``` -------------------------------- ### Compare jq and gojq regex behavior Source: https://github.com/jqlang/jq/wiki/FAQ This example highlights discrepancies in regex handling between jq 1.6 and gojq, specifically with `gsub` and the `test` function regarding end-of-string matching. ```bash echo gsub: for jq in jq-1.6 gojq do echo $jq : $($jq -n '"aaa" | gsub("^a"; "b")' ) done echo test: for jq in jq-1.6 gojq do echo $jq : $($jq -n '"abc\n" | test("c$")') done ``` ```text gsub: jq-1.6 : "bbb" gojq : "baa" test: jq-1.6 : true gojq : false ``` -------------------------------- ### Cartesian Product Behavior Example in jq Source: https://github.com/jqlang/jq/wiki/Advanced-Topics Illustrates the Cartesian product behavior of some jq expressions. Note the difference in output between using `,` and `;` with `range`, and between `and` with comma-separated values versus piped values. ```jq range(0,1;3,4) ``` ```jq range(0;3), range(0;4), range(1;3), range(1;4) ``` ```jq "\( (true, false) ) and \( (true, false) )" ``` ```jq (true, false) and (true, false) ``` -------------------------------- ### Set up Bison for maintainer mode build Source: https://github.com/jqlang/jq/wiki/Installation Installs Bison version 3 using Homebrew and sets it as the primary version in the PATH. Required for building jq in maintainer mode. ```bash $ brew install bison # Ensure that this bison has priority than the system version. $ export PATH="$(brew --prefix)/opt/bison/bin:$PATH" ``` -------------------------------- ### Using path(..) to get all paths Source: https://github.com/jqlang/jq/wiki/jq-Language-Description Demonstrates how to use the `path/1` built-in with the `..` operator to extract all possible paths within a JSON object. This is useful for understanding the structure of the data. ```jq $ printf '%s\n' '{"a":{"b":[{"c":0},{"d":1}]}}' | jq -c 'path(..)' [] ["a"] ["a","b"] ["a","b",0] ["a","b",0,"c"] ["a","b",1] ["a","b",1,"d"] ``` -------------------------------- ### Illustrative JSON Object Source: https://github.com/jqlang/jq/wiki/For-JSONPath-users The sample JSON object used throughout the examples to demonstrate JSONPath and jq queries. ```json { "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } } } ``` -------------------------------- ### Build jq from source with built-in oniguruma Source: https://github.com/jqlang/jq/wiki/Installation Builds jq from source using the built-in oniguruma regex library. Requires autoconf, automake, and libtool. Ensure dependencies are installed. ```bash cd modules/oniguruma autoreconf -fi cd ../.. autoreconf -i ./configure --with-oniguruma=builtin make ``` -------------------------------- ### Lookbehind with Alternatives Example Source: https://github.com/jqlang/jq/wiki/Docs-for-Oniguruma-Regular-Expressions-(RE.txt) Illustrates the use of lookbehind assertions with alternatives. Note that the subexpression within a lookbehind must be of fixed character length. ```Regex (?<=a|bc) ``` ```Regex (?<=aaa(?:b|cd)) ``` -------------------------------- ### Convert JSON5 to JSON with json5 Source: https://github.com/jqlang/jq/wiki/FAQ Install and use the json5 command-line tool to convert JSON5 files to standard JSON. The output file is named based on the input filename. ```bash npm install json5 ln -s ~/node_modules/.bin/json5 ~/bin json5 -c FILENAME.json5 # generates FILENAME.json ``` -------------------------------- ### Example: Summing numbers with reduce in jq Source: https://github.com/jqlang/jq/wiki/jq-Language-Description Demonstrates using the 'reduce' primitive to sum numbers from a range. The update expression '. + $n' is applied to the accumulated state, with '$n' representing each successive input from 'range(5)'. ```jq reduce range(5) as $n (0; .+$n) ``` -------------------------------- ### Build jq Website Source: https://github.com/jqlang/jq/blob/master/docs/README.md Navigate to the `docs/` directory and execute this command to build the jq project website. This script compiles the website assets. ```bash pipenv run python3 build_website.py ``` -------------------------------- ### Backreference by Group Number Example Source: https://github.com/jqlang/jq/wiki/Docs-for-Oniguruma-Regular-Expressions-(RE.txt) A basic example of backreferencing a captured group using its number. ```Regex \1 ``` -------------------------------- ### Serve jq Website Locally Source: https://github.com/jqlang/jq/blob/master/docs/README.md After building the website, use this command to serve the generated output locally. This allows you to preview the website before deployment. ```bash python3 -m http.server -d output ``` -------------------------------- ### jq Mathematical Function Examples Source: https://github.com/jqlang/jq/wiki/FAQ Demonstrates the usage of various mathematical functions in jq, including trigonometric, logarithmic, and special functions like nan and infinite. Note that some functions are 0-arity filters. ```jq atan2(1;1), hypot(3;4), pow(2;3), remainder(5; -2) ``` ```jq nan | isnan ``` ```jq infinite | isinfinite ``` -------------------------------- ### Prepare build environment Source: https://github.com/jqlang/jq/wiki/Cross-compilation Commands to prepare the build environment, including autoreconf and configure. ```shell : ; autoreconf -fi : ; ./configure : ; make distclean ``` -------------------------------- ### jq Tail-Call Optimization (TCO) Example Source: https://github.com/jqlang/jq/wiki/Advanced-Topics Demonstrates tail-call optimization using a local recursive helper function '_range'. This pattern is necessary for TCO in jq versions 1.5rc1 and up, especially for functions without arguments. ```jq def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else . end; if by == 0 then init else init|_range end | select((by > 0 and . < upto) or (by < 0 and . > upto)) ; ``` -------------------------------- ### Test Case Format Source: https://github.com/jqlang/jq/wiki/Docs-and-tests Defines the simple format for test cases in '.test' files: program on one line, input on the next, and output(s) on subsequent lines. Test cases are terminated by a blank line. Lines starting with '#' are comments. ```text # This is a comment .foo {"foo": "bar"} "bar" .baz | . + 1 {"baz": 5} 6 ``` -------------------------------- ### JJ Path Syntax with Filters vs. JQ Source: https://github.com/jqlang/jq/wiki/For-JJ-users Demonstrates JJ path syntax with filtering conditions and their JQ equivalents, focusing on array element selection based on properties. ```shell friends.#[last=="Murphy"].first >> .friends[] | first(select(.last=="Murphy")) | .first friends.#[last=="Murphy"]#.first >> .friends[] | select(.last=="Murphy") | .first friends.#[age>45]#.last >> .friends[] | select(.age > 45) | .last friends.#[first%"D*"].last >> .friends[] | first(select(.first|startswith("D"))) | .last # or test("^D") ``` -------------------------------- ### Import JSON data using jq_each in SQLite Source: https://github.com/jqlang/jq/wiki/FAQ Demonstrates importing data from a JSON file into SQLite using the jq_each table-valued function. This example assumes the JSON file contains a single object. ```sql insert into raw_data select * from jq_each(readfile('airline_routes.json'), '.[]|to_entries[] | {(.key): .value}'); ``` -------------------------------- ### Prepare Release Workspace Source: https://github.com/jqlang/jq/wiki/Release-Production Clone the repository, checkout the master branch, clean the workspace, and pull the latest changes and tags. Ensure autoreconf is run to correctly configure the build. ```bash cd ~/workspaces # or whatever git clone jq-rel cd jq-rel git checkout -f master git clean -fx git pull -u git pull --tags autoreconf -fi ./configure ... && make dist ``` -------------------------------- ### jq 'or' vs '//' operator examples Source: https://github.com/jqlang/jq/wiki/FAQ Illustrates the behavior of the 'or' and '//' operators in jq. '//' returns the first truthy value or the second expression's stream, while 'or' has different short-circuiting and stream concatenation semantics. ```jq (null,1) or (2,3) ``` ```jq (null, 1, null,2) // (10, 20) ``` -------------------------------- ### Build jq from source in maintainer mode Source: https://github.com/jqlang/jq/wiki/Installation Builds jq from source in maintainer mode. Requires oniguruma, autoconf, automake, and libtool installed via Homebrew. Ensure correct Bison version is prioritized. ```bash $ mkdir github $ cd github $ git clone https://github.com/jqlang/jq.git $ cd jq; autoreconf -i ; ./configure ; make ``` -------------------------------- ### Lazy Evaluation with `limit/2` in jq Source: https://github.com/jqlang/jq/wiki/jq-Language-Description Demonstrates `limit/2` which outputs only the first N values from its second argument. The example shows that even a large range is efficiently processed due to this limiting behavior. ```jq [limit(5; range(1000000))] ``` -------------------------------- ### Import and Use Library Function in JQ Source: https://github.com/jqlang/jq/wiki/Cookbook Demonstrates how to import a JQ library file using the -L option and then use a function from that library. Ensure the library path is correctly specified. ```shell jq -L $HOME/jq 'import "library" as lib; lib::walk(if type == "object" then del(.foo) else . end)' <<< '{"a":1, "foo": 2}' ``` -------------------------------- ### NUL-Separated Output with --raw-output0 Source: https://github.com/jqlang/jq/blob/master/NEWS.md Utilize the --raw-output0 option for NUL (zero byte) separated output, which is useful for piping to tools like xargs -0. Examples show its usage with xxd, xargs, and a while loop with read. ```sh # will output a zero byte after each output $ jq -n --raw-output0 '1,2,3' | xxd 00000000: 3100 3200 3300 1.2.3. ``` ```sh # can be used with xargs -0 $ jq -n --raw-output0 '"a","b","c"' | xargs -0 -n1 a b c ``` ```sh $ jq -n --raw-output0 '"a b c", "d\ne\nf"' | xargs -0 printf '[%s]\n' [a b c] [d e f] ``` ```sh # can be used with read -d '' $ while IFS= read -r -d '' json; do > jq '.name' <<< "$json" > done < <(jq -n --raw-output0 '{name:"a b c"},{name:"d\ne\nf"}') "a b c" "d\ne\nf" ``` ```sh # also it's an error to output a string containing a NUL when using NUL separator $ jq -n --raw-output0 '"\u0000"' jq: error (at ): Cannot dump a string containing NUL with --raw-output0 option ``` -------------------------------- ### Enable Documentation Builds Source: https://github.com/jqlang/jq/blob/master/docs/README.md Run this command in the jq root directory to enable documentation generation. This modifies the build configuration to include documentation targets. ```bash ./configure --enable-docs ``` -------------------------------- ### Rename keys using translate_keys (Example 2) Source: https://github.com/jqlang/jq/wiki/FAQ Example demonstrating how to conditionally rename a specific key ('c' to 'C') in a JSON entity using the `translate_keys` function. ```jq {"a": 1, "b": [{"c":2}] } | translate_keys( if . == "c" then "C" else . end ) ``` -------------------------------- ### Include and use a library function in jq Source: https://github.com/jqlang/jq/wiki/Cookbook Demonstrates how to include a jq library file and use its functions, such as 'walk/1', for data transformation. ```shell jq 'include "library"; walk(if type == "object" then del(.foo) else . end)' <<< '{"a":1, "foo": 2}' ``` -------------------------------- ### JJ Path Syntax Equivalents to JQ Source: https://github.com/jqlang/jq/wiki/For-JJ-users Illustrates basic JJ path syntax and its corresponding JQ expressions for accessing JSON elements. ```shell @this >> . @this.0 >> .[0] name.last >> .name.last age >> .age children >> .children children.# >> .children|length children.1 >> .children[1] child*.2 >> to_entries[] | select(.key|test("^child")) | .value[2] c?ildren.0 >> to_entries[] | select(.key|test("^c.ildren$")) | .value[0] fav\.movie >> .["fav.movie"] # alternatively: ."fav.movie" friends.#.first >> [.friends[].first] . # or: .friends | map(.first) friends.1.last >> .friends[1].last ``` -------------------------------- ### jq number precision example Source: https://github.com/jqlang/jq/wiki/FAQ jq versions prior to 1.7 convert numbers to IEEE 754 64-bit values, potentially causing loss of precision. This example demonstrates how large numbers are handled. ```jq jqMaster -M -n '123456789123456789123456789123456789123456789123456789.0000|[.,tostring]' ``` -------------------------------- ### Subexpression Call by Group Number Example Source: https://github.com/jqlang/jq/wiki/Docs-for-Oniguruma-Regular-Expressions-(RE.txt) Demonstrates calling a subexpression using its group number. ```Regex \g ``` -------------------------------- ### Build and Test jq Source: https://github.com/jqlang/jq/wiki/Guide-for-Contributors Build the jq project and run its tests using autoreconf, configure, and make. Ensure maintainer mode is enabled and Oniguruma is built internally. ```bash $ autoreconf -fi $ ./configure --enable-maintainer-mode --with-oniguruma=builtin $ make -j4 check ``` -------------------------------- ### Backreference by Group Name Example Source: https://github.com/jqlang/jq/wiki/Docs-for-Oniguruma-Regular-Expressions-(RE.txt) Demonstrates backreferencing a captured group using its assigned name. ```Regex \k ``` -------------------------------- ### Subexpression Call by Group Name Example Source: https://github.com/jqlang/jq/wiki/Docs-for-Oniguruma-Regular-Expressions-(RE.txt) Shows calling a subexpression using its assigned group name. ```Regex \g ``` -------------------------------- ### Accessing the First Two Books in Store Source: https://github.com/jqlang/jq/wiki/For-JSONPath-users Directly selects the first two books from the 'store.book' array. ```jq .store.book[0:2] ``` -------------------------------- ### Create and Activate Virtual Environment Source: https://github.com/jqlang/jq/blob/master/docs/README.md In some cases, you might need to manually create a virtual environment and activate it before syncing dependencies with pipenv. This isolates project dependencies. ```bash virtualenv -p /usr/bin/python3 venv/ source venv/bin/activate ``` -------------------------------- ### Build jq from Source Source: https://github.com/jqlang/jq/wiki/Installation Compiles jq from its source code using autoconf and make. This is a standard method for building from source. ```bash cd ~/jq autoreconf -i ./configure make -j8 sudo make install ``` -------------------------------- ### Subexpression Call by Relative Group Name Example Source: https://github.com/jqlang/jq/wiki/Docs-for-Oniguruma-Regular-Expressions-(RE.txt) Demonstrates calling a subexpression using its name and relative level adjustments. ```Regex \g ``` ```Regex \g ``` -------------------------------- ### Backreference by Relative Group Name Example Source: https://github.com/jqlang/jq/wiki/Docs-for-Oniguruma-Regular-Expressions-(RE.txt) Illustrates backreferencing a named group using its name and relative level adjustments. ```Regex \k ``` ```Regex \k ``` -------------------------------- ### Backreference by Relative Group Number Example Source: https://github.com/jqlang/jq/wiki/Docs-for-Oniguruma-Regular-Expressions-(RE.txt) Shows backreferencing a group using its relative number, adjusting for nesting levels. ```Regex \k ``` ```Regex \k ``` -------------------------------- ### Get Current Time Source: https://github.com/jqlang/jq/wiki/jq-Language-Description The `now` function returns the current time as a Unix timestamp. This is a side-effecting function as the output changes with each execution. ```jq now ``` -------------------------------- ### jq Module Imports, Includes, and Function Definitions Source: https://github.com/jqlang/jq/wiki/jq-Language-Description Demonstrates the structure for module imports, includes, and function definitions in a jq program, followed by the main expression. ```jq # Module imports, includes, and function definitions: import "a" as foo; include "b"; def some_function: body_here; # ... # # Finally, the main program, really, a singular expression: some_expression | some_other_expression # and so on # But note that you can have `def ...` in any expression. ``` -------------------------------- ### Configure Bat for jq Syntax Highlighting Source: https://github.com/jqlang/jq/wiki/bat-language-syntax These commands set up Bat to recognize and highlight jq syntax. It involves creating a directory for custom syntaxes, instructing Bat to use a specific YAML file for the jq syntax definition, and then rebuilding Bat's cache. Finally, it demonstrates highlighting a simple jq expression. ```sh $ mkdir -p "$(bat --config-dir)/syntaxes" $ echo "paste below yaml into $(bat --config-dir)/syntaxes/jq.sublime-syntax" $ bat cache --build $ echo 'def f: 1+2;' | bat -p -l jq ``` -------------------------------- ### Get unique keys from all objects in a list Source: https://github.com/jqlang/jq/wiki/Cookbook Extracts all unique top-level keys from all JSON objects within a given array. ```jq [.. | objects | keys[]] | unique ``` -------------------------------- ### Get Array Element with jv_array_get Source: https://github.com/jqlang/jq/wiki/C-API:-jv Demonstrates the standard usage of jv_array_get to retrieve an element from a jv array at a specific index. ```C jv elem = jv_array_get(array, 42); ``` -------------------------------- ### List All jq Built-in Functions Source: https://github.com/jqlang/jq/wiki/jq-Language-Description Use this command to list all available built-in functions in jq. The output is a JSON array of function names. ```bash jq -nr 'builtins[]' ``` -------------------------------- ### Accessing Object Keys in jq 1.3 Source: https://github.com/jqlang/jq/wiki/FAQ In jq 1.3, object keys are sorted by default. This example demonstrates how keys are processed. ```jq jq -n '{b:1, a:2} | to_entries[].key' ``` -------------------------------- ### Recursive Subexpression Call Example Source: https://github.com/jqlang/jq/wiki/Docs-for-Oniguruma-Regular-Expressions-(RE.txt) Demonstrates a left-most recursive call within a named group. This pattern is disallowed and will result in an error. ```Regex (?a|\gb) ``` -------------------------------- ### Subexpression Call by Relative Group Number Example Source: https://github.com/jqlang/jq/wiki/Docs-for-Oniguruma-Regular-Expressions-(RE.txt) Illustrates calling a subexpression using its relative group number, adjusting for nesting levels. ```Regex \g<-n> ``` ```Regex \g ``` ```Regex \g ``` -------------------------------- ### Valid Recursive Subexpression Call Example Source: https://github.com/jqlang/jq/wiki/Docs-for-Oniguruma-Regular-Expressions-(RE.txt) Illustrates a valid recursive call within a named group, where the recursive part is not the left-most alternative. ```Regex (?a|b\gc) ``` -------------------------------- ### Run jq with Docker Source: https://github.com/jqlang/jq/blob/master/README.md Extracts the version from a package.json file using Docker. Ensure the package.json file is accessible. ```bash docker run --rm -i ghcr.io/jqlang/jq:latest < package.json '.version' ``` -------------------------------- ### Array Slice in jq Source: https://github.com/jqlang/jq/wiki/jq-Language-Description Extracts a portion of an array based on start and end expressions. This operator allows for slicing arrays similar to Python. ```jq term[start_expr : end_expr] ``` -------------------------------- ### Collect Stream to Array in jq Source: https://github.com/jqlang/jq/wiki/Advanced-Topics Collects the output stream of an expression into an array. Useful for aggregating results. For example, `[range(.;.*2)]` collects integers. ```jq [0,1] ``` ```jq [range(.;.*2)] ``` -------------------------------- ### Create an array of n zeros using range and reduce Source: https://github.com/jqlang/jq/wiki/FAQ An alternative method to create an array of a specified size filled with zeros, utilizing `range` and `reduce`. ```jq reduce range(0;n) as $i ([]; . + [0]) ``` -------------------------------- ### Convert Hjson to JSON with npm package Source: https://github.com/jqlang/jq/wiki/FAQ Install the hjson package globally via npm to convert Hjson files to JSON using the -j flag. ```bash npm install hjson -g hjson -j file.hjson # to convert to JSON ``` -------------------------------- ### jq limit() with multiple limiters Source: https://github.com/jqlang/jq/wiki/Advanced-Topics Demonstrates a potentially surprising behavior of `limit()` when provided with multiple limit values, resulting in repeated outputs. ```jq limit(1,2; range(0;10)) 0 0 1 ``` -------------------------------- ### jq Template: Using $variables Source: https://github.com/jqlang/jq/wiki/Cookbook Instantiates a jq template file using '$'-variables. This method is suitable for a small number of variables and requires all variables to be explicitly provided. ```jsonc {a: $a, b: $a} ``` ```shell jq -n --argjson a 0 -f ab.jq ``` -------------------------------- ### jq 1.5+ Behavior with empty in reduce Source: https://github.com/jqlang/jq/wiki/FAQ This example shows the behavior of `empty` within a `reduce` statement, which might be surprising and could change in future versions. ```jq jq -n 'reduce 2 as $x (3; empty)' ``` -------------------------------- ### Negative Lookbehind with Shy Group Example Source: https://github.com/jqlang/jq/wiki/Docs-for-Oniguruma-Regular-Expressions-(RE.txt) Shows a negative lookbehind assertion that uses a non-capturing (shy) group. Captured groups are not allowed in negative lookbehinds. ```Regex (? create table input ( json JSON ); sqlite> .mode line sqlite> .separator "\t" sqlite> .import "|jq -c .[] input.json" input sqlite> select * from input limit 1; json = {"key_one":1,"key_two":2,"key_three":3} sqlite> ``` -------------------------------- ### Input Filename: Get Current Input File Source: https://github.com/jqlang/jq/wiki/jq-Language-Description The `input_filename` function returns the name of the file currently being processed. This is useful when dealing with multiple input files. ```jq input_filename ``` -------------------------------- ### Get Keys of Store Object Source: https://github.com/jqlang/jq/wiki/For-JSONPath-users This jq query retrieves the keys of the 'store' object, which are 'bicycle' and 'book'. This is used in conjunction with other queries to access elements by key. ```jq .store | keys ``` -------------------------------- ### Make jq script executable with shebang (Recommended) Source: https://github.com/jqlang/jq/wiki/FAQ This approach uses a shell script wrapper for jq scripts, ensuring portability across Unix-like systems. It correctly handles arguments and the script file itself. ```sh #!/bin/sh -- # \ exec jq -Mf "$0" "$@" # jq code follows .your | .filter | .here ```