5 JMESPath (pronounced "jaymz path") allows you to declaratively specify how to
6 extract elements from a JSON document. *jmespath.php* allows you to use
7 JMESPath in PHP applications with PHP data structures. It requires PHP 5.4 or
8 greater and can be installed through `Composer <http://getcomposer.org/doc/00-intro.md>`_
9 using the ``mtdowling/jmespath.php`` package.
13 require 'vendor/autoload.php';
15 $expression = 'foo.*.baz';
19 'bar' => ['baz' => 1],
20 'bam' => ['baz' => 2],
25 JmesPath\search($expression, $data);
28 - `JMESPath Tutorial <http://jmespath.org/tutorial.html>`_
29 - `JMESPath Grammar <http://jmespath.org/specification.html#grammar>`_
30 - `JMESPath Python library <https://github.com/jmespath/jmespath.py>`_
35 The ``JmesPath\search`` function can be used in most cases when using the
36 library. This function utilizes a JMESPath runtime based on your environment.
37 The runtime utilized can be configured using environment variables and may at
38 some point in the future automatically utilize a C extension if available.
42 $result = JmesPath\search($expression, $data);
44 // or, if you require PSR-4 compliance.
45 $result = JmesPath\Env::search($expression, $data);
50 jmespath.php utilizes *runtimes*. There are currently two runtimes:
51 AstRuntime and CompilerRuntime.
53 AstRuntime is utilized by ``JmesPath\search()`` and ``JmesPath\Env::search()``
59 The AstRuntime will parse an expression, cache the resulting AST in memory,
60 and interpret the AST using an external tree visitor. AstRuntime provides a
61 good general approach for interpreting JMESPath expressions that have a low to
62 moderate level of reuse.
66 $runtime = new JmesPath\AstRuntime();
67 $runtime('foo.bar', ['foo' => ['bar' => 'baz']]);
73 ``JmesPath\CompilerRuntime`` provides the most performance for
74 applications that have a moderate to high level of reuse of JMESPath
75 expressions. The CompilerRuntime will walk a JMESPath AST and emit PHP source
76 code, resulting in anywhere from 7x to 60x speed improvements.
78 Compiling JMESPath expressions to source code is a slower process than just
79 walking and interpreting a JMESPath AST (via the AstRuntime). However,
80 running the compiled JMESPath code results in much better performance than
81 walking an AST. This essentially means that there is a warm-up period when
82 using the ``CompilerRuntime``, but after the warm-up period, it will provide
83 much better performance.
85 Use the CompilerRuntime if you know that you will be executing JMESPath
86 expressions more than once or if you can pre-compile JMESPath expressions
87 before executing them (for example, server-side applications).
91 // Note: The cache directory argument is optional.
92 $runtime = new JmesPath\CompilerRuntime('/path/to/compile/folder');
93 $runtime('foo.bar', ['foo' => ['bar' => 'baz']]);
99 You can utilize the CompilerRuntime in ``JmesPath\search()`` by setting
100 the ``JP_PHP_COMPILE`` environment variable to "on" or to a directory
101 on disk used to store cached expressions.
106 A comprehensive list of test cases can be found at
107 https://github.com/jmespath/jmespath.php/tree/master/tests/compliance.
108 These compliance tests are utilized by jmespath.php to ensure consistency with
109 other implementations, and can serve as examples of the language.
111 jmespath.php is tested using PHPUnit. In order to run the tests, you need to
112 first install the dependencies using Composer as described in the *Installation*
113 section. Next you just need to run the tests via make:
119 You can run a suite of performance tests as well: