package Mojo::Response::JSON::Path;
# ABSTRACT: use JSON::Path for searching JSON responses
use Class::Method::Modifiers qw/install_modifier/;
use Mojo::Message;
use JSON::Path;
sub import {
my
hift;
install_modifier "Mojo::Message", 'around', 'json',
sub {
my $orig = shift;
my $self = shift;
if (@_) {
return JSON::Path->new($_[0])->value($orig->($self));
} else
=head1 Mojo::Response::JSON::Path - use JSON::Path for searching JSON responses
=head2 SYNOPSIS
use Mojo::Response::JSON::Path;
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
;
my $json = $tx->res->json('$.entities');
=head2 DESCRIPTION
This module allows the use of an optional JSON Path expression to extract a specific value from a Mojo::Message via JSON::Path.
=head2 FUNCTIONS
None
=head2 SEE ALSO
Related modules: L<Mojo::Message>, L<Mojo::JSON::Pointer>, L<JSON::Path>
=head2 AUTHORS
Simone Cesano
=head2 COPYRIGHT AND LICENSE
This software is copyri
in/env perl
use Mojolicious::Lite;
plugin 'Config';
get '/' => sub {
my $c = shift;
my $json = {
"entities" => {
"Q100148272" => {
"id" => "Q100148272",
"sitelinks" =
"type" => "item"
}
},
"success" => 1
};
$c->render(json => $json);
};
app->start;
__DATA__
@@ index.html.ep
% layout 'default';
% title 'Welcome';
<h1>Wel
use Mojo::Util qw/dumper/;
use Mojo::Response::JSON::Path;
use Mojo::UserAgent;
use Time::HiRes qw/time/;
$\ = "\n"; $, = "\t";
my $ua = Mojo::UserAgent->new;
my $url = Mojo::URL->new("http://local
;
my $json = $tx->res->json('$.entities');
print dumper $json;
}
print time() - $start;
# print dumper $json;
# print dumper $tx->res->json('//title');
# my $u = Weirdo->new($json);
# pri
ies&format=json&props=sitelinks&ids=Q19675&sitefilter=enwiki");
my $id = "Q100148272";
$url->query({ ids => $id });
$tx = $ua->get($url);
my $json = $tx->res->json();
print dumper $json;
print du
mper $tx->res->json('//title');
my $u = Weirdo->new($json);
print dumper $u->get('$.entities.Q100148272.sitelinks.enwiki.title');
print dumper $u->get('..title');