Overview of the jql crate
mainjql crate provides the binary tool for jql. It is the primary entry point for users looking to use the jql command-line interface.repository·main·Indexed 23 days ago
https://github.com/yamafaktory/jqlA fast, lightweight JSON Query Language tool written in Rust designed to process JSON input and output JSON. It provides a command-line interface and Rust crates (jql, jql-parser, jql-runner) for selecting, flattening, and transforming data using tokens and operators such as key selectors, array/object ranges, flatten (..), and pipe operators.
jql crate provides the binary tool for jql. It is the primary entry point for users looking to use the jql command-line interface.A Lens selector is a combination of one or more selectors with an optional value. A value can be any of boolean | null | number | string. It is used to filter elements based on specific criteria.
Input:
[
{ "a": 1, "b": { "d": 2 } },
{ "a": 2, "b": "some" },
{ "a": 2, "b": { "d": null } },
{ "a": 2, "b": true },
{ "c": 3, "b": 4 }
]Query: '|={ "b" "d"=2, "c" }'
Output:
[
{ "a": 1, "b": { "d": 2 } },
{ "c": 3, "b": 4 }
]You can select data from objects using keys, multiple keys, or index-based ranges.
Any valid JSON key can be used (must be double-quoted).
Input: { "a": 1, "b": 2, "c": 3 }
Query: '"c"'
Output: 3
Keys can be used in arbitrary order.
Input: { "a": 1, "b": 2, "c": 3 }
Query: '{"c","a"}'
Output: { "c": 3, "a": 1 }
Indexes can be used in arbitrary order.
Input: { "a": 1, "b": 2, "c": 3 }
Query: '{2,0}'
Output: { "c": 3, "a": 1 }
Range can be in natural order {0:2}, reversed {2:0}, or use open bounds {:2} or {0:}.
Input: { "a": 1, "b": 2, "c": 3 }
Query: '{2:1}'
Output: { "c": 3, "b": 2 }
..)Flattens arrays and objects. For objects, it produces a flattened map with dot-notation keys.
Input (Array): [[[[{{"a": 1}}]]]]
Query: '..'
Output: [{"a": 1}]
Input (Object): { "a": { "c": false }, "b": { "d": { "e": { "f": 1 } } } }
Query: '..'
Output: { "a.c": false, "b.d.e.f": 1 }
@)Returns the keys of an object or the indices of an array. Other primitives are returned as is.
Input: { "a": 1, "b": 2, "c": 3 }
Query: '@'
Output: ["a", "b", "c"]
|> )Applies the next tokens in parallel on each element of an array.
Input: { "a": [{ "b": { "c": 1 } }, { "b": { "c": 2 } }] }
Query: '"a"|>"b""c"'
Output: [1, 2]
<| )Stops the parallelization initiated by the pipe in operator.
Input: { "a": [{ "b": { "c": 1 } }, { "b": { "c": 2 } }] }
Query: '"a"|>"b""c"<|[1]'
Output: 2
!)Maps the output into simple JSON primitives: boolean | null | number | string | [] | {}.
Input: { "a": [1, 2, 3] }
Query: '"a"!'
Output: []
You can select elements from arrays using indexes or ranges.
Indexes can be used in arbitrary order.
Input: [1, 2, 3]
Query: '[2,1]'
Output: [3, 2]
Ranges can be in natural order [0:2], reversed [2:0], or use open bounds like [:2] or [0:].
Input: [1, 2, 3]
Query: '[2:1]'
Output: [3, 2]
You can install jql using various package managers depending on your operating system:
apk add jqlyay -S jqldnf install jqlpkg install jqlbrew install jqlnix-env -i jqlzypper install jqlcargo install jql or cargo binstall jqlAlternatively, you can download compiled binaries manually from the GitHub releases page.
cargo install jqlTo use jql, provide a query as a sequence of tokens. Because jql requires key selectors to be double-quoted to comply with JSON standards, you should enclose your queries in single quotes in the shell to avoid escaping issues.
Save output to a file:
jql '"a"' input.json > output.jsonRead from stdin:
cat test.json | jql '"a"'jql '"a"' input.json > output.jsonThe jql-runner crate provides the core execution logic for JQL (JSON Query Language). It is organized into several modules, primarily exposing runner for execution logic and errors for handling query-related failures.
To use the runner programmatically, you will primarily interact with the runner module to execute queries against JSON data.
The ! (TruncateOperator) is a special operator in JQL used to truncate results. It has strict usage rules enforced by the parser:
! in a single query.! must be the last token in the query.If these rules are violated, the parser returns a JqlParserError::TruncateError containing the stringified tokens.
The jql binary allows you to query JSON data using a query language. It supports reading JSON from a file, from standard input (stdin), or via a stream of JSON lines. You can provide the query directly as an argument or read it from a file.
--json-file flag.jql via stdin. By default, it reads the entire piped content as a single JSON object/array.--stream flag to process input line-by-line (NDJSON/JSONL format). Each line is treated as an independent JSON object.--inline to output compact, colorized JSON.--raw-string to output the result as a plain string if the query result is a JSON string.--validate to check if the input is valid JSON without executing a query.The jql command-line tool allows you to query JSON data using a specific query language. You can provide a query as a positional argument, read it from a file, or use it to validate JSON data.
jql '"your_query"' input.json| Argument | Short | Long | Description |
|---|---|---|---|
query | Positional argument. The query to apply to the JSON data. Must be enclosed in single quotes. | ||
json_file | Positional argument. The JSON file to use. | ||
query_from_file | -q | --query | Read the query from a file instead of a positional argument. |
inline | -i | --inline | Inline the JSON output. |
raw-string | -r | --raw-string | Write to stdout without JSON double-quotes (string only). |
stream | -s | --stream | Read a stream of JSON data line by line. |
validate | -v | --validate | Validate the JSON data (does not require a query). |
--query (file) and query (positional) are mutually exclusive with --validate.query positional argument is required unless --query (file) or --validate is provided.The following flags are available for the jql command:
| Flag | Long Flag | Description |
|---|---|---|
-i | --inline | Inline the JSON output (disables pretty printing) |
-q | --query <FILE> | Read the query from a file instead of stdin |
-r | --raw-string | Write to stdout without JSON double-quotes (useful for string primitives) |
-s | --stream | Read a stream of JSON data line by line |
-v | --validate | Validate the JSON content and return a matching exit code |
-h | --help | Print help |
-V | --version | Print version |