Add JMESPath support for querying JSON
This adds hypervel/jmespath, a package for querying decoded JSON with JMESPath expressions. An application can use it to let someone choose fields or filter records without writing PHP for each query.
For example, this returns the IDs of orders that meet a threshold from the same document:
use Hypervel\JmesPath\Facades\JmesPath;
$ids = JmesPath::search(
'let $minimum = minimum in orders[?total >= $minimum].id',
$document,
);
A filter changes the current value to each order. The let binding keeps the document’s threshold available inside that filter. Bindings belong to the search, so a compiled expression can be reused across documents and concurrent requests.
Running queries in a worker
Compilation limits the size, nesting, and node count of an expression. Each search has a work budget, including work performed inside a projection or through a variable binding. The result also has a size limit. Workers cache compiled expressions in memory up to a configurable byte limit.
By default, JSON objects keep their stdClass representation, including empty objects and numeric property names. Applications that decode JSON as associative arrays can select the matching mode. Applications can also register their own functions at boot. PHP callbacks run as application code and are responsible for their own work.
Using the package
The package provides a facade and an injectable factory, a validation rule for submitted expressions, and an Artisan command for searching JSON from a file or standard input. The guide covers configuration, storing and reusing expressions, and handling syntax and evaluation errors.
The tests run the upstream compliance fixtures in both JSON modes and exercise lexical scope, work limits, cache reuse, coroutine isolation, validation, and the command. The package suite, static analysis, and formatting checks passed locally.
[!NOTE]
Add JMESPath support for querying JSON via new hypervel/jmespath package
- Adds a new JMESPath library with an AST compiler, expression factory with caching, facade, and service provider. Compiles expressions into closures with configurable limits: 16,384 bytes, 512 nodes, depth 32, 250,000 work units, and a 16 MB expression cache.
- Adds a
jmespath:search console command that reads JSON from stdin or --file, supports associative decoding, --ast output, and a work-unit budget (SearchCommand.php).
- Adds a
JmesPathExpression validation rule for request validation and a SyntaxErrorException that reports character position and line of parse errors (JmesPathExpression.php).
- Includes full unit, compliance, and budget test suites based on the upstream JMESPath compliance fixtures (tests/JmesPath).
- Risk: the compliance fixtures escape.json and literal.json contain invalid JSON (plus signs between fields), so a JSON-based compliance loader will fail on them.
Macroscope summarized b0ee798.
Summary by CodeRabbit
- New Features
- Added JMESPath support for querying JSON data with expressions, including standard and custom functions.
- Added expression validation, associative-array or object results, and configurable expression and evaluation limits.
- Added a console command to search JSON from a file or standard input.
- Added a validation rule for JMESPath expressions and support for accessing searches through the application facade.
- Documentation
- Added guides covering installation, configuration, searching, validation, custom functions, and the console command.