JSON::Path::Evaluator(3pm) | User Contributed Perl Documentation | JSON::Path::Evaluator(3pm) |
JSON::Path::Evaluator - A module that recursively evaluates JSONPath expressions with native support for Javascript-style filters
version 1.0.3
use JSON::MaybeXS qw/decode_json/; # Or whatever JSON thing you like. I won't judge. use JSON::Path::Evaluator qw/evaluate_jsonpath/; my $obj = decode_json(q( { "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } } } )); my @fiction = evaluate_jsonpath( $obj, q{$..book[?(@.category == "fiction")]}); # @fiction = ( # { category => "fiction", # author => "Evelyn Waugh", # title => "Sword of Honour", # price => 12.99 # }, # { category => "fiction", # author => "Herman Melville", # title => "Moby Dick", # isbn => "0-553-21311-3", # price => 8.99 # }, # { category => "fiction", # author => "J. R. R. Tolkien", # title => "The Lord of the Rings", # isbn => "0-395-19395-8", # price => 22.99 # } # );
Constructor for the object-oriented interface to this module. Arguments may be specified in a hash or a hashref.
Args:
Evaluate a JSONPath expression on the given object. CLASS METHOD.
Args:
Evaluate a JSONPath expression on the object passed to the constructor. OBJECT METHOD.
Args:
Supported keys:
This code implements the JSONPath specification at JSONPath specification <http://goessner.net/articles/JsonPath/>.
JSONPath is a tool, similar to XPath for XML, that allows one to construct queries to pick out parts of a JSON structure.
From the spec: "JSONPath expressions always refer to a JSON structure in the same way as XPath expression are used in combination with an XML document. Since a JSON structure is usually anonymous and doesn't necessarily have a "root member object" JSONPath assumes the abstract name $ assigned to the outer level object."
Note that in JSONPath square brackets operate on the object or array addressed by the previous path fragment. Indices always start by 0.
Filters are the most powerful feature of JSONPath. They allow the caller to retrieve data conditionally, similar to Perl's "grep" operator.
Filters are specified using the '?(' token, terminated by ')'. Anything in between these two tokens is treated as a filter expression. Filter expressions must return a boolean value.
Filtering with PseudoJS
By default, this module uses a limited subset of Javascript expressions to evaluate filters. Using this script engine, specify the filter in the form "<LHS> <operator> <RHS>", or "<LHS>". This latter case will be evaluated as "<LHS> is true".
<LHS> must be a valid JSONPath expression. <RHS> must be a scalar value; comparison of two JSONPath expressions is not supported at this time.
Example:
Using the JSON in SYNOPSIS above and the JSONPath expression "$..book[?(@.category == "fiction")]", the filter expression "@.category == "fiction"" will match all values having a value of "fiction" for the key "category".
Regular expressions are supported using the "=~" operator, for example: "$..book[?(@.category =~ /Fiction/i)]". This is an extension of the Goessner specification <http://goessner.net/articles/JsonPath/> introduced by Jayway JsonPath <https://github.com/json-path/JsonPath>. Other Jayway operators are not currently supported.
When the script engine is set to "perl", filter Using the JSON in SYNOPSIS above and the JSONPath expression "$..book[?(@.category == "fiction")]",
This is understandably dangerous. Although steps have been taken (Perl expressions are evaluated using Safe and a limited set of permitted opcodes) to reduce the risk, callers should be aware of the risk when using filters.
When filtering in Perl, there are some differences between the JSONPath spec and this implementation.
Kit Peters <popefelix@gmail.com>
This software is copyright (c) 2022 by Kit Peters.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
2022-12-27 | perl v5.36.0 |