 # JQ::Lite [](https://metacpan.org/pod/JQ::Lite) [](https://github.com/kawamurashingo/JQ-Lite) **JQ::Lite** is a lightweight, pure-Perl JSON query engine inspired by the [`jq`](https://stedolan.github.io/jq/) command-line tool. It allows you to extract, traverse, and filter JSON data using a simplified jq-like syntax โ entirely within Perl. --- ## ๐ง Features - โ **Pure Perl** (no XS, no external binaries) - โ Dot notation (`.users[].name`) - โ Optional key access (`.nickname?`) - โ Array indexing and expansion (`.users[0]`, `.users[]`) - โ `select(...)` filters with `==`, `!=`, `<`, `>`, `and`, `or` - โ Built-in functions: `length`, `keys`, `first`, `last`, `reverse`, `sort`, `unique`, `has`,`map` - โ Command-line interface: `jq-lite` - โ Reads from STDIN or file - โ **Interactive mode** for exploring JSON line-by-line - โ **v0.19+:** `--use` option to select decoder (JSON::PP, JSON::XS, etc.) - โ **v0.19+:** `--debug` option to show active JSON module --- ## ๐ค Why JQ::Lite (vs `jq` or `JSON::PP`)? | Use Case | Tool | |-----------------------|-----------------| | Simple JSON decode | โ `JSON::PP` | | Shell processing | โ `jq` | | jq-style queries in Perl | โ **JQ::Lite** | | Lightweight & portable | โ **JQ::Lite** | --- ## ๐ฆ Installation ```sh perl Makefile.PL make make test make install ``` --- ## ๐ Usage ### As a Perl module ```perl use JQ::Lite; my $json = '{"users":[{"name":"Alice"},{"name":"Bob"}]}'; my $jq = JQ::Lite->new; my @names = $jq->run_query($json, '.users[].name'); print join("\n", @names), "\n"; ``` ### As a command-line tool ```bash cat users.json | jq-lite '.users[].name' jq-lite '.users[] | select(.age > 25)' users.json jq-lite -r '.users[].name' users.json ``` for windows ```powershell type user.json | jq-lite ".users[].name" jq-lite -r ".users[].name" users.json ``` > โ ๏ธ `jq-lite` is named to avoid conflict with the real `jq`. --- ### ๐ Interactive Mode If you omit the query, `jq-lite` enters **interactive mode**, allowing you to type queries line-by-line against a fixed JSON input. ```bash jq-lite users.json ```  ``` jq-lite interactive mode. Enter query (empty line to quit): > .users[0].name "Alice" > .users[] | select(.age > 25) { "name" : "Alice", "age" : 30, ... } ``` - Results will be **re-rendered each time**, clearing the previous output (like a terminal UI). - Works with `--raw-output` (`-r`) as well. --- ### ๐ Decoder selection and debug output If installed, the following JSON modules are checked and used in order of priority: `JSON::MaybeXS`, `Cpanel::JSON::XS`, `JSON::XS`, and `JSON::PP`. You can see which module is being used with the --debug option. ```bash $ jq-lite --debug .users[0].age users.json [DEBUG] Using Cpanel::JSON::XS 30 $ jq-lite --use JSON::PP .users[0].age users.json 30 $ jq-lite --use JSON::PP --debug .users[0].age users.json [DEBUG] Using JSON::PP 30 ``` --- ## ๐ Example Input ```json { "users": [ { "name": "Alice", "age": 30, "profile": { "active": true, "country": "US" } }, { "name": "Bob", "age": 25, "profile": { "active": false, "country": "JP" } } ] } ``` ### Example Queries ```bash jq-lite '.users[].name' users.json jq-lite '.users | length' users.json jq-lite '.users[0] | keys' users.json jq-lite '.users[].nickname?' users.json jq-lite '.users[] | select(.age > 25)' users.json jq-lite '.users[] | select(.profile.active == true) | .name' users.json jq-lite '.users | sort | reverse | first' users.json ``` --- ## ๐งช Testing ```bash prove -l t/ ``` --- ## ๐ฆ CPAN ๐ [JQ::Lite on MetaCPAN](https://metacpan.org/pod/JQ::Lite) --- ## ๐ License This module is released under the same terms as Perl itself. --- ## ๐ค Author **Kawamura Shingo** ๐ง pannakoota1@gmail.com ๐ [GitHub @kawamurashingo](https://github.com/kawamurashingo/JQ-Lite)