.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "JSON::Path 3pm" .TH JSON::Path 3pm "2021-01-31" "perl v5.32.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" JSON::Path \- search nested hashref/arrayref structures using JSONPath .SH "VERSION" .IX Header "VERSION" version 0.431 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 10 \& 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 \*(Aqjpath_map\*(Aq; \& \& # All books in the store \& my $jpath = JSON::Path\->new(\*(Aq$.store.book[*]\*(Aq); \& my @books = $jpath\->values($data); \& \& # The author of the last (by order) book \& my $jpath = JSON::Path\->new(\*(Aq$..book[\-1:].author\*(Aq); \& my $tolkien = $jpath\->value($data); \& \& # Convert all authors to uppercase \& jpath_map { uc $_ } $data, \*(Aq$.store.book[*].author\*(Aq; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module implements JSONPath, an XPath-like language for searching JSON-like structures. .PP JSONPath is described at . .SS "Constructor" .IX Subsection "Constructor" .ie n .IP """JSON::Path\->new($string)""" 4 .el .IP "\f(CWJSON::Path\->new($string)\fR" 4 .IX Item "JSON::Path->new($string)" Given a JSONPath expression \f(CW$string\fR, returns a JSON::Path object. .SS "Methods" .IX Subsection "Methods" .ie n .IP """values($object)""" 4 .el .IP "\f(CWvalues($object)\fR" 4 .IX Item "values($object)" Evaluates the JSONPath expression against an object. The object \f(CW$object\fR can be either a nested Perl hashref/arrayref structure, or a \s-1JSON\s0 string capable of being decoded by JSON::MaybeXS::decode_json. .Sp Returns a list of structures from within \f(CW$object\fR which match against the JSONPath expression. In scalar context, returns the number of matches. .ie n .IP """value($object)""" 4 .el .IP "\f(CWvalue($object)\fR" 4 .IX Item "value($object)" Like \f(CW\*(C`values\*(C'\fR, but returns just the first value. This method is an lvalue sub, which means you can assign to it: .Sp .Vb 3 \& my $person = { name => "Robert" }; \& my $path = JSON::Path\->new(\*(Aq$.name\*(Aq); \& $path\->value($person) = "Bob"; .Ve .Sp \&\s-1TAKE NOTE\s0! This will create keys in \f(CW$object\fR. E.G.: .Sp .Vb 3 \& my $obj = { foo => \*(Aqbar\*(Aq }; \& my $path = JSON::Path\->new(\*(Aq$.baz\*(Aq); \& $path\->value($obj) = \*(Aqbak\*(Aq; # $obj\->{baz} is created and set to \*(Aqbak\*(Aq; .Ve .ie n .IP """paths($object)""" 4 .el .IP "\f(CWpaths($object)\fR" 4 .IX Item "paths($object)" As per \f(CW\*(C`values\*(C'\fR but instead of returning structures which match the expression, returns canonical JSONPaths that point towards those structures. .ie n .IP """get($object)""" 4 .el .IP "\f(CWget($object)\fR" 4 .IX Item "get($object)" In list context, identical to \f(CW\*(C`values\*(C'\fR, but in scalar context returns the first result. .ie n .IP """set($object, $value, $limit)""" 4 .el .IP "\f(CWset($object, $value, $limit)\fR" 4 .IX Item "set($object, $value, $limit)" Alters \f(CW$object\fR, setting the paths to \f(CW$value\fR. If set, then \&\f(CW$limit\fR limits the number of changes made. .Sp \&\s-1TAKE NOTE\s0! This will create keys in \f(CW$object\fR. E.G.: .Sp .Vb 3 \& my $obj = { foo => \*(Aqbar\*(Aq }; \& my $path = JSON::Path\->new(\*(Aq$.baz\*(Aq); \& $path\->set($obj, \*(Aqbak\*(Aq); # $obj\->{baz} is created and set to \*(Aqbak\*(Aq .Ve .Sp Returns the number of changes made. .ie n .IP """map($object, $coderef)""" 4 .el .IP "\f(CWmap($object, $coderef)\fR" 4 .IX Item "map($object, $coderef)" Conceptually similar to Perl's \f(CW\*(C`map\*(C'\fR keyword. Executes the coderef (in scalar context!) for each match of the path within the object, and sets a new value from the coderef's return value. Within the coderef, \f(CW$_\fR may be used to access the old value, and \f(CW$.\fR may be used to access the curent canonical JSONPath. .ie n .IP """to_string""" 4 .el .IP "\f(CWto_string\fR" 4 .IX Item "to_string" Returns the original JSONPath expression as a string. .Sp This method is usually not needed, as the JSON::Path should automatically stringify itself as appropriate. i.e. the following works: .Sp .Vb 2 \& my $jpath = JSON::Path\->new(\*(Aq$.store.book[*].author\*(Aq); \& print "I\*(Aqm looking for: " . $jpath . "\en"; .Ve .SS "Functions" .IX Subsection "Functions" The following functions are available for export, but are not exported by default: .ie n .IP """jpath($object, $path_string)""" 4 .el .IP "\f(CWjpath($object, $path_string)\fR" 4 .IX Item "jpath($object, $path_string)" Shortcut for \f(CW\*(C`JSON::Path\->new($path_string)\->values($object)\*(C'\fR. .ie n .IP """jpath1($object, $path_string)""" 4 .el .IP "\f(CWjpath1($object, $path_string)\fR" 4 .IX Item "jpath1($object, $path_string)" Shortcut for \f(CW\*(C`JSON::Path\->new($path_string)\->value($object)\*(C'\fR. Like \f(CW\*(C`value\*(C'\fR, it can be used as an lvalue. .ie n .IP """jpath_map { CODE } $object, $path_string""" 4 .el .IP "\f(CWjpath_map { CODE } $object, $path_string\fR" 4 .IX Item "jpath_map { CODE } $object, $path_string" Shortcut for \f(CW\*(C`JSON::Path\->new($path_string)\->map($object, $code)\*(C'\fR. .SH "NAME" JSON::Path \- search nested hashref/arrayref structures using JSONPath .SH "PERL SPECIFICS" .IX Header "PERL SPECIFICS" 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... .SS "JSONPath Embedded Perl Expressions" .IX Subsection "JSONPath Embedded Perl Expressions" JSONPath expressions may contain subexpressions that are evaluated using the native host language. e.g. .PP .Vb 1 \& $..book[?($_\->{author} =~ /tolkien/i)] .Ve .PP The stuff between \*(L"?(\*(R" and \*(L")\*(R" 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: .PP .Vb 1 \& $JSON::Path::Safe = 0; .Ve .PP There are some differences between the JSONPath spec and this implementation. .IP "\(bu" 4 JSONPath uses a variable '$' to refer to the root node. This is not a legal variable name in Perl, so '$root' is used instead. .IP "\(bu" 4 JSONPath uses a variable '@' to refer to the current node. This is not a legal variable name in Perl, so '$_' is used instead. .SS "Blessed Objects" .IX Subsection "Blessed Objects" 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: .PP .Vb 3 \& Scalar::Util::blessed($object) \& && $object\->can(\*(Aqtypeof\*(Aq) \& && $object\->typeof =~ /^(ARRAY|HASH)$/ .Ve .PP which are treated as an unblessed arrayref or hashref appropriately. .SH "BUGS" .IX Header "BUGS" Please report any bugs to . .SH "SEE ALSO" .IX Header "SEE ALSO" Specification: . .PP Implementations in \s-1PHP,\s0 Javascript and C#: . .PP Related modules: \s-1JSON\s0, \s-1JSON::JOM\s0, \s-1JSON::T\s0, \s-1JSON::GRDDL\s0, JSON::Hyper, JSON::Schema. .PP 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. .SH "AUTHOR" .IX Header "AUTHOR" Toby Inkster . .SH "MAINTAINER" .IX Header "MAINTAINER" Kit Peters .SH "CONTRIBUTORS" .IX Header "CONTRIBUTORS" Szymon NieznaƄski .PP Kit Peters .PP Heiko Jansen . .PP Mitsuhiro Nakamura .SH "COPYRIGHT AND LICENCE" .IX Header "COPYRIGHT AND LICENCE" Copyright 2007 Stefan Goessner. .PP Copyright 2010\-2013 Toby Inkster. .PP This module is tri-licensed. It is available under the X11 (a.k.a. \s-1MIT\s0) licence; you can also redistribute it and/or modify it under the same terms as Perl itself. .ie n .SS "a.k.a. ""The \s-1MIT\s0 Licence""" .el .SS "a.k.a. ``The \s-1MIT\s0 Licence''" .IX Subsection "a.k.a. The MIT Licence" Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \*(L"Software\*(R"), 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: .PP The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. .PP \&\s-1THE SOFTWARE IS PROVIDED \*(L"AS IS\*(R", 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.\s0 .SH "AUTHOR" .IX Header "AUTHOR" Kit Peters .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2021 by Kit Peters. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.