use strict;
use warnings;
use Test::More;
use JQ::Lite;
my $json = q({
"users": [
{ "name": "Alice", "email": "alice@example.com" },
{ "name": "Bob", "email": "bob@admin.com" },
{ "na
q->run_query($json, '.users[] | select(.name match "Bob")');
my @matched_admin = $jq->run_query($json, '.users[] | select(.email match "^bob@")');
my @matched_fail = $jq->run_query($json, '.users[] |
w(trm);
use CallBackery::Exception qw(mkerror);
use Text::CSV;
use Excel::Writer::XLSX;
use Mojo::JSON qw(true false);
use Time::Piece;
=head1 NAME
CallBackery::GuiPlugin::AbstractTable - Base Class
late qw(trm);
use CallBackery::Exception qw(mkerror);
use Mojo::Util qw(hmac_sha1_sum);
use Mojo::JSON qw(true false);
=head1 NAME
CallBackery::GuiPlugin::UserForm - UserForm Plugin
=head1 SYNOPSIS
te;
my $json = q({
"score": "42",
"raw": "n/a",
"flag": true,
"nested": [["10", "oops"], [false, "0"]],
"maybe": null
});
my $jq = JQ::Lite->new;
my @scalar = $jq->run_query($json, '.scor
my @boolean = $jq->run_query($json, '.flag | to_number');
is($boolean[0], 1, 'to_number converts JSON booleans to numeric values');
my @string = $jq->run_query($json, '.raw | to_number');
is($strin
ery($json, '.nested | to_number');
is_deeply(
$array[0],
[[10, 'oops'], [0, 0]],
'to_number recurses through arrays preserving non-numeric entries'
);
my @maybe = $jq->run_query($json, '.
rnings;
use Test::More tests => 4;
use JSON::PP;
use JQ::Lite;
my $jq = JQ::Lite->new;
# --- 1. Empty array
my $json1 = '[]';
my @result1 = $jq->run_query($json1, 'is_empty');
ok($result1[0], 'is_em
n-empty array
my $json2 = '[1,2,3]';
my @result2 = $jq->run_query($json2, 'is_empty');
ok(!$result2[0], 'is_empty() returns false for non-empty array');
# --- 3. Empty hash
my $json3 = '{}';
my @resu
$jq->run_query($json3, 'is_empty');
ok($result3[0], 'is_empty() returns true for empty hash');
# --- 4. Non-empty hash
my $json4 = '{"key":"value"}';
my @result4 = $jq->run_query($json4, 'is_empty');
use Test::More;
use JQ::Lite;
my $json = q([
{ "value": 10 },
{ "value": 30 },
{ "value": 20 }
]);
my $jq = JQ::Lite->new;
my ($add) = $jq->run_query($json, 'map(.value) | add');
is($add, 60
jq->run_query($json, 'map(.value) | sum');
is($sum, 60, 'sum = 60');
my ($min) = $jq->run_query($json, 'map(.value) | min');
is($min, 10, 'min = 10');
my ($max) = $jq->run_query($json, 'map(.value)
s($max, 30, 'max = 30');
my ($avg) = $jq->run_query($json, 'map(.value) | avg');
is($avg, 20, 'avg = 20');
my ($variance) = $jq->run_query($json, 'map(.value) | variance');
ok(abs($variance - 66.666
ey)->then(sub($result) {
$self->render(json => { data => $result->{data}, result => => 1 });
})->catch(sub($err) {
$self->render(json => { 'result' => 0, data => $err });
})->w
p()->then(sub($result) {
$self->render(json => { data => $result->{data}, result => => 1 });
})->catch(sub($err) {
$self->render(json => { 'result' => 0, data => $err });
})->w
More;
use JQ::Lite;
my $json = q({
"nums": [5, 3, 5, 1, 3],
"words": ["banana", "apple", "banana", "pear"]
});
my $jq = JQ::Lite->new;
my @sorted = $jq->run_query($json, '.nums | sort');
my @un
use Encode qw(decode);
use JQ::Lite;
my $json = decode('UTF-8', '"こんにちは!"');
my $jq = JQ::Lite->new;
my @results = eval { $jq->run_query($json, '@uri') };
my $error = $@;
is($error,
::More tests => 5;
use JSON::PP;
use JQ::Lite;
my $jq = JQ::Lite->new;
# --- 1. Nested object with arrays and booleans
my $json1 = '{"user":{"name":"Alice","tags":["perl","json"],"active":true}}';
m
y @result1 = $jq->run_query($json1, 'paths');
is_deeply(
$result1[0],
[
[ 'user' ],
[ 'user', 'active' ],
[ 'user', 'name' ],
[ 'user', 'tags' ],
[ 'use
nested key/index in order',
);
# --- 2. Mixed array contents
my $json2 = '[1,{"foo":[2,null]}]';
my @result2 = $jq->run_query($json2, 'paths');
is_deeply(
$result2[0],
[
[ 0 ],
n/../lib";
use JQ::Lite;
my $jq = JQ::Lite->new;
my $json_nested = <<'JSON';
[[1, 2], [3, [4, 5]], 6]
JSON
my @depth_one = $jq->run_query($json_nested, 'flatten_depth(1)');
is_deeply(
$depth_on
n_query($json_nested, 'flatten_depth(2)');
is_deeply(
$depth_two[0],
[1, 2, 3, 4, 5, 6],
'flatten_depth(2) flattens two levels of nesting'
);
my @depth_zero = $jq->run_query($json_nested,
>run_query($json_nested, 'flatten_depth');
is_deeply(
$default_depth[0],
[1, 2, 3, [4, 5], 6],
'flatten_depth defaults to a depth of 1 when omitted'
);
my $json_mixed = <<'JSON';
[[{"name
ew;
my $json = <<'JSON';
{
"users": [
{ "name": "Alice", "age": 30 },
{ "name": "Bob", "age": 22 },
{ "name": "Carol", "age": 19 }
]
}
JSON
my @res1 = $jq->run_query($json, '.users
| count');
is_deeply(\@res1, [3], 'count users');
my @res2 = $jq->run_query($json, '.users[] | select(.age > 25) | count');
is_deeply(\@res2, [1], 'count users over 25');
done_testing;
gs;
use Test::More;
use JQ::Lite;
my $json = <<'JSON';
{
"items": [[1, 2], [3], [], [4, 5]]
}
JSON
my $jq = JQ::Lite->new;
my @results = $jq->run_query($json, '.items | flatten');
is_deeply(\@res
}\n",
);
is($stdout, "[{\"name\":\"Alice\"},{\"name\":\"Bob\"}]\n", '--slurp aggregates multiple JSON documents');
like($stderr, qr/^\s*\z/, 'no warnings emitted when slurping multiple documents');
i
=> "{\"name\":\"Carol\"}\n",
);
is($stdout, "[{\"name\":\"Carol\"}]\n", '--slurp wraps a single JSON document in an array');
like($stderr, qr/^\s*\z/, 'no warnings emitted when slurping a single doc
rnings;
use Test::More;
use JQ::Lite;
my $json = '{"row":["foo","bar","baz,qux","he said \"hi\""]}';
my $jq = JQ::Lite->new;
my @row = $jq->run_query($json, '.row | @csv');
is_deeply(\@row, ['"foo"
jq->run_query($json, '.row[0] | @csv');
is_deeply(\@single, ['"foo"'], '@csv formats scalar values as CSV fields');
my $mixed_json = '["42", 42]';
my @mixed = $jq->run_query($mixed_json, '@csv');
is_
package JQ::Lite;
use strict;
use warnings;
use JSON::PP ();
use JQ::Lite::Filters;
use JQ::Lite::Parser;
use JQ::Lite::Util ();
our $VERSION = '1.43';
sub new {
my ($class, %opts) = @_;
return bless $self, $class;
}
sub run_query {
my ($self, $json_text, $query) = @_;
my $data = JQ::Lite::Util::_decode_json($json_text);
return ($data) if !defined $query || $query =~ /^\
A lightweight jq-like JSON query engine in Perl
=head1 VERSION
Version 1.43
=head1 SYNOPSIS
use JQ::Lite;
my $jq = JQ::Lite->new;
my @results = $jq->run_query($json_text, '.users[].name'
)->then(sub($result) {
$self->render(json => { data => $result->{data}, result => => 1 });
})->catch(sub($err) {
$self->render(json => { 'result' => 0, data => $err });
})->w
= qw(Exporter);
our @EXPORT = qw();
use strict;
use warnings;
use 5.010;
use HTTP::Tinyish;
use JSON;
use FindBin qw( $RealBin $RealScript );
use File::Copy;
use File::Spec::Functions qw( catfile ca
($res->{success}) {
say STDERR "Couldn\'t fetch release index at $release_index_url. Error: $res->{status} $res->{reason}";
;
exit 1;
}
return decode_json($res->{content});
}
ey)->then(sub($result) {
$self->render(json => { data => $result->{data}, result => => 1 });
})->catch(sub($err) {
$self->render(json => { 'result' => 0, data => $err });
})->w
ey)->then(sub($result) {
$self->render(json => { data => $result->{data}, result => => 1 });
})->catch(sub($err) {
$self->render(json => { 'result' => 0, data => $err });
})->w