Giter Site home page Giter Site logo

jmespath.php's Introduction

jmespath.php

JMESPath (pronounced "jaymz path") allows you to declaratively specify how to extract elements from a JSON document. jmespath.php allows you to use JMESPath in PHP applications with PHP data structures. It requires PHP 5.4 or greater and can be installed through Composer using the mtdowling/jmespath.php package.

require 'vendor/autoload.php';

$expression = 'foo.*.baz';

$data = [
    'foo' => [
        'bar' => ['baz' => 1],
        'bam' => ['baz' => 2],
        'boo' => ['baz' => 3]
    ]
];

JmesPath\search($expression, $data);
// Returns: [1, 2, 3]

PHP Usage

The JmesPath\search function can be used in most cases when using the library. This function utilizes a JMESPath runtime based on your environment. The runtime utilized can be configured using environment variables and may at some point in the future automatically utilize a C extension if available.

$result = JmesPath\search($expression, $data);

// or, if you require PSR-4 compliance.
$result = JmesPath\Env::search($expression, $data);

Runtimes

jmespath.php utilizes runtimes. There are currently two runtimes: AstRuntime and CompilerRuntime.

AstRuntime is utilized by JmesPath\search() and JmesPath\Env::search() by default.

AstRuntime

The AstRuntime will parse an expression, cache the resulting AST in memory, and interpret the AST using an external tree visitor. AstRuntime provides a good general approach for interpreting JMESPath expressions that have a low to moderate level of reuse.

$runtime = new JmesPath\AstRuntime();
$runtime('foo.bar', ['foo' => ['bar' => 'baz']]);
// > 'baz'

CompilerRuntime

JmesPath\CompilerRuntime provides the most performance for applications that have a moderate to high level of reuse of JMESPath expressions. The CompilerRuntime will walk a JMESPath AST and emit PHP source code, resulting in anywhere from 7x to 60x speed improvements.

Compiling JMESPath expressions to source code is a slower process than just walking and interpreting a JMESPath AST (via the AstRuntime). However, running the compiled JMESPath code results in much better performance than walking an AST. This essentially means that there is a warm-up period when using the CompilerRuntime, but after the warm-up period, it will provide much better performance.

Use the CompilerRuntime if you know that you will be executing JMESPath expressions more than once or if you can pre-compile JMESPath expressions before executing them (for example, server-side applications).

// Note: The cache directory argument is optional.
$runtime = new JmesPath\CompilerRuntime('/path/to/compile/folder');
$runtime('foo.bar', ['foo' => ['bar' => 'baz']]);
// > 'baz'
Environment Variables

You can utilize the CompilerRuntime in JmesPath\search() by setting the JP_PHP_COMPILE environment variable to "on" or to a directory on disk used to store cached expressions.

Custom functions

The JMESPath language has numerous built-in functions, but it is also possible to add your own custom functions. Keep in mind that custom function support in jmespath.php is experimental and the API may change based on feedback.

If you have a custom function that you've found useful, consider submitting it to jmespath.site and propose that it be added to the JMESPath language. You can submit proposals here.

To create custom functions:

  • Create any callable structure (loose function or class with functions) that implement your logic.
  • Call FnDispatcher::registerCustomFunction() to register your function. Be aware that these registerCustomFunction() calls must be in a global place if you want to have your functions always available.

Here is an example with a class instance:

// Create a class that contains your function
class CustomFunctionHandler
{
    public function double($args)
    {
        return $args[0] * 2;
    }
}
FnDispatcher::registerCustomFunction('myFunction', [new CustomFunctionHandler(), 'double'])

An example with a runtime function:

$callbackFunction = function ($args) {
    return $args[0];
};
FnDispatcher::registerCustomFunction('myFunction', $callbackFunction);

As you can see, you can use all the possible callable structures as defined in the PHP documentation. All those examples will lead to a function myFunction() that can be used in your expressions.

Type specification

The FnDispatcher::registerCustomFunction() function accepts an optional third parameter that allows you to pass an array of type specifications for your custom function. If you pass this, the types (and count) of the passed parameters in the expression will be validated before your callable is executed.

Example:

FnDispatcher::registerCustomFunction('myFunction', $callbackFunction, [['number'], ['string']]);

Defines that your function expects exactly 2 parameters, the first being a number and the second being a string. If anything else is passed in the call to your function, a \RuntimeException will be thrown.

Testing

A comprehensive list of test cases can be found at https://github.com/jmespath/jmespath.php/tree/master/tests/compliance. These compliance tests are utilized by jmespath.php to ensure consistency with other implementations, and can serve as examples of the language.

jmespath.php is tested using PHPUnit. In order to run the tests, you need to first install the dependencies using Composer as described in the Installation section. Next you just need to run the tests via make:

make test

You can run a suite of performance tests as well:

make perf

jmespath.php's People

Contributors

mtdowling avatar grahamcampbell avatar narcoticfresh avatar nickfan avatar jamesls avatar whatthejeff avatar jeremeamia avatar mfn avatar pborreli avatar morozov avatar siwinski avatar kawaz avatar datashaman avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.