|
NAMEJSON::Path::Evaluator - A module that recursively evaluates JSONPath expressions with native support for Javascript-style filtersVERSIONversion 0.5SYNOPSISuse 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 # } # ); METHODSnewConstructor for the object-oriented interface to this module. Arguments may be specified in a hash or a hashref.Args:
evaluate_jsonpathEvaluate a JSONPath expression on the given object. CLASS METHOD.Args:
evaluateEvaluate a JSONPath expression on the object passed to the constructor. OBJECT METHOD.Args:
JSONPathThis 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. JSONPath ExpressionsFrom 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. Operators
FilteringFilters 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". Filtering with PerlWhen 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.
AUTHORKit Peters <popefelix@gmail.com>COPYRIGHT AND LICENSEThis software is copyright (c) 2021 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. POD ERRORSHey! The above document had some coding errors, which are explained below:
Visit the GSP FreeBSD Man Page Interface. |