JSON::Path(3pm) | User Contributed Perl Documentation | JSON::Path(3pm) |
JSON::Path - search nested hashref/arrayref structures using JSONPath
version 0.431
my $data = { "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, }, ], }, }; use JSON::Path 'jpath_map'; # All books in the store my $jpath = JSON::Path->new('$.store.book[*]'); my @books = $jpath->values($data); # The author of the last (by order) book my $jpath = JSON::Path->new('$..book[-1:].author'); my $tolkien = $jpath->value($data); # Convert all authors to uppercase jpath_map { uc $_ } $data, '$.store.book[*].author';
This module implements JSONPath, an XPath-like language for searching JSON-like structures.
JSONPath is described at <http://goessner.net/articles/JsonPath/>.
Returns a list of structures from within $object which match against the JSONPath expression. In scalar context, returns the number of matches.
my $person = { name => "Robert" }; my $path = JSON::Path->new('$.name'); $path->value($person) = "Bob";
TAKE NOTE! This will create keys in $object. E.G.:
my $obj = { foo => 'bar' }; my $path = JSON::Path->new('$.baz'); $path->value($obj) = 'bak'; # $obj->{baz} is created and set to 'bak';
TAKE NOTE! This will create keys in $object. E.G.:
my $obj = { foo => 'bar' }; my $path = JSON::Path->new('$.baz'); $path->set($obj, 'bak'); # $obj->{baz} is created and set to 'bak'
Returns the number of changes made.
This method is usually not needed, as the JSON::Path should automatically stringify itself as appropriate. i.e. the following works:
my $jpath = JSON::Path->new('$.store.book[*].author'); print "I'm looking for: " . $jpath . "\n";
The following functions are available for export, but are not exported by default:
JSON::Path - search nested hashref/arrayref structures using JSONPath
JSONPath is intended as a cross-programming-language method of searching nested object structures. There are however, some things you need to think about when using JSONPath in Perl...
JSONPath expressions may contain subexpressions that are evaluated using the native host language. e.g.
$..book[?($_->{author} =~ /tolkien/i)]
The stuff between "?(" and ")" is a Perl expression that must return a boolean, used to filter results. As arbitrary Perl may be used, this is clearly quite dangerous unless used in a controlled environment. Thus, it's disabled by default. To enable, set:
$JSON::Path::Safe = 0;
There are some differences between the JSONPath spec and this implementation.
Blessed objects are generally treated as atomic values; JSON::Path will not follow paths inside them. The exception to this rule are blessed objects where:
Scalar::Util::blessed($object) && $object->can('typeof') && $object->typeof =~ /^(ARRAY|HASH)$/
which are treated as an unblessed arrayref or hashref appropriately.
Please report any bugs to <http://rt.cpan.org/>.
Specification: <http://goessner.net/articles/JsonPath/>.
Implementations in PHP, Javascript and C#: <http://code.google.com/p/jsonpath/>.
Related modules: JSON, JSON::JOM, JSON::T, JSON::GRDDL, JSON::Hyper, JSON::Schema.
Similar functionality: Data::Path, Data::DPath, Data::SPath, Hash::Path, Path::Resolver::Resolver::Hash, Data::Nested, Data::Hierarchy... yes, the idea's not especially new. What's different is that JSON::Path uses a vaguely standardised syntax with implementations in at least three other programming languages.
Toby Inkster <tobyink@cpan.org>.
Kit Peters <popefelix@cpan.org>
Szymon Nieznański <s.nez@member.fsf.org>
Kit Peters <popefelix@cpan.org>
Heiko Jansen <hjansen@cpan.org>.
Mitsuhiro Nakamura <m.nacamura@gmail.com>
Copyright 2007 Stefan Goessner.
Copyright 2010-2013 Toby Inkster.
This module is tri-licensed. It is available under the X11 (a.k.a. MIT) licence; you can also redistribute it and/or modify it under the same terms as Perl itself.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Kit Peters <popefelix@gmail.com>
This 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.
2021-01-31 | perl v5.32.0 |