| 1 | use 5.010scip-perl cpan JSON-Path 1.0.6 `5.010`/; |
| 2 | use JSON::MaybeXSscip-perl cpan JSON-Path 1.0.6 JSON/MaybeXS/; |
| 3 | use Scalar::Utilscip-perl cpan JSON-Path 1.0.6 Scalar/Util/ qw[blessed]; |
| 4 | use libscip-perl cpan JSON-Path 1.0.6 lib/ "../lib"; |
| 5 | use JSON::Pathscip-perl cpan JSON-Path 1.0.6 JSON/Path/; |
| 6 | |
| 7 | my $json = JSON::MaybeXS->newscip-perl cpan JSON-Path 1.0.6 JSON/MaybeXS/new().( pretty => 1 ); |
| 8 | my $object = $json->decodescip-perl cpan JSON-Path 1.0.6 JSON/MaybeXS/decode().(<<'JSON'); |
| 9 | { |
| 10 | "store": { |
| 11 | "book": [ |
| 12 | { |
| 13 | "category": "reference", |
| 14 | "author": "Nigel Rees", |
| 15 | "title": "Sayings of the Century", |
| 16 | "price": 8.95 |
| 17 | }, |
| 18 | { |
| 19 | "category": "fiction", |
| 20 | "author": "Evelyn Waugh", |
| 21 | "title": "Sword of Honour", |
| 22 | "price": 12.99 |
| 23 | }, |
| 24 | { |
| 25 | "category": "fiction", |
| 26 | "author": "Herman Melville", |
| 27 | "title": "Moby Dick", |
| 28 | "isbn": "0-553-21311-3", |
| 29 | "price": 8.99 |
| 30 | }, |
| 31 | { |
| 32 | "category": "fiction", |
| 33 | "author": "J. R. R. Tolkien", |
| 34 | "title": "The Lord of the Rings", |
| 35 | "isbn": "0-395-19395-8", |
| 36 | "price": 22.99 |
| 37 | } |
| 38 | ], |
| 39 | "bicycle": { |
| 40 | "color": "red", |
| 41 | "price": 19.95 |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | JSON |
| 46 | |
| 47 | $JSON::Path::Safescip-perl cpan JSON-Path 1.0.6 JSON/Path/$Safe. = 0; |
| 48 | |
| 49 | foreach ('$.store.book[0].title', '$.store.book[*].author', '$..author', '$..book[-1:]', |
| 50 | '$..book[?($_->{author} =~ /tolkien/i)]') |
| 51 | { |
| 52 | my $jpath = JSON::Path->newscip-perl cpan JSON-Path 1.0.6 JSON/Path/new().($_scip-perl cpan JSON-Path 1.0.6 main/$_.); |
| 53 | sayscip-perl cpan JSON-Path 1.0.6 main/say(). $jpath; |
| 54 | sayscip-perl cpan JSON-Path 1.0.6 main/say(). $json->encodescip-perl cpan JSON-Path 1.0.6 JSON/MaybeXS/encode().([$jpath->valuesscip-perl cpan JSON-Path 1.0.6 JSON/Path/values().($object)]); |
| 55 | sayscip-perl cpan JSON-Path 1.0.6 main/say(). $json->encodescip-perl cpan JSON-Path 1.0.6 JSON/MaybeXS/encode().([$jpath->pathsscip-perl cpan JSON-Path 1.0.6 JSON/Path/paths().($object)]); |
| 56 | sayscip-perl cpan JSON-Path 1.0.6 main/say(). [$jpath->valuesscip-perl cpan JSON-Path 1.0.6 JSON/Path/values().($object)]->[0]->nodePathscip-perl cpan JSON-Path 1.0.6 main/nodePath(). |
| 57 | if blessedscip-perl cpan JSON-Path 1.0.6 main/blessed().([$jpath->valuesscip-perl cpan JSON-Path 1.0.6 JSON/Path/values().($object)]->[0]); |
| 58 | sayscip-perl cpan JSON-Path 1.0.6 main/say(). '-' x 40; |
| 59 | } |