use strict;
use warnings;
use Test::More;
use JQ::Lite;
my $json = q({
"tags": ["perl", "json", "perl", "cli"],
"phrase": "banana",
"users": [
{"name": "Alice"},
{"name": "Bob"},
{"
es = $jq->run_query($json, '.tags | indices("perl")');
is_deeply($tag_indices[0], [0, 2], 'indices() finds all matching array positions');
my @user_indices = $jq->run_query($json, '.users | indices({
dices = $jq->run_query($json, '.numbers | indices(2)');
is_deeply($number_indices[0], [1, 3], 'indices() handles numeric comparisons');
my @bool_indices = $jq->run_query($json, '.mixed | indices(true
$json = <<'JSON';
{
"items": [
"text",
42,
true,
null,
{"note": "object"},
[1, 2, 3]
],
"flag": false
}
JSON
my $jq = JQ::Lite->new;
my @results = $jq->run_query($json,
;
isa_ok($results[2], 'JSON::PP::Boolean', 'scalars preserves boolean objects');
ok(!defined $results[3], 'scalars keeps null values as undef');
@results = $jq->run_query($json, '.items | scalars');
alars yields no output for array containers');
@results = $jq->run_query($json, '.flag | scalars');
isa_ok($results[0], 'JSON::PP::Boolean', 'scalars passes through scalar booleans');
ok(!$results[0]
use strict;
use warnings;
use Test::More;
use JSON::PP;
use JQ::Lite;
my $json = <<'JSON';
{
"items": [
{ "name": "Widget", "value": 1 },
{ "value": 2, "active": true },
{ "nested": { "
],
"single": { "foo": "bar" },
"no_hashes": ["alpha", "beta"]
}
JSON
my $jq = JQ::Lite->new;
my @merged = $jq->run_query($json, '.items | merge_objects');
is_deeply(
\@merged,
[
e => JSON::PP::true,
nested => { id => 42 },
}
],
'merge_objects folds arrays of objects into one hash with later values winning',
);
my @single = $jq->run_query($json, '.
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;
Q::Lite;
my $json = q({
"nums": [1, 9, 3, 2],
"words": ["apple", "pear", "banana"],
"mixed": ["10", "2", "30"]
});
my $jq = JQ::Lite->new;
my @desc_nums = $jq->run_query($json, '.nums | sor
t_desc');
my @desc_words = $jq->run_query($json, '.words | sort_desc');
my @desc_mixed = $jq->run_query($json, '.mixed | sort_desc');
is_deeply($desc_nums[0], [9, 3, 2, 1], 'numeric sort descendin
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 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
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');
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_
Test::More tests => 4;
use FindBin;
use lib "$FindBin::Bin/../lib";
use JQ::Lite;
use JSON::PP;
my $json = <<'EOF';
{
"users": [
{ "name": "Alice", "age": 30 },
{ "name": "Bob", "
$jq->run_query($json, '.users[] | .name');
is_deeply(\@names, ["Alice", "Bob", "Charlie"], 'Extract names from users array');
# Test: users[].age
my @ages = $jq->run_query($json, '.users[] | .a
n_query($json, '.users[].name');
is_deeply(\@alt, ["Alice", "Bob", "Charlie"], 'Alternative syntax for extracting names');
# Test: top-level object access
my @all = $jq->run_query($json, '.user
my $json = q({
"numbers": [1, 2, 3, 4],
"user": { "name": "Alice" }
});
my $jq = JQ::Lite->new;
my @drop_0 = $jq->run_query($json, '.numbers | drop(0)');
my @drop_2 = $jq->run_query($json, '.n
umbers | drop(2)');
my @drop_5 = $jq->run_query($json, '.numbers | drop(5)');
my @non_array = $jq->run_query($json, '.user | drop(1)');
is_deeply($drop_0[0], [1, 2, 3, 4], 'drop(0) keeps all elements
tests => 3;
use JSON::PP;
use JQ::Lite;
my $jq = JQ::Lite->new;
# --- 1. Existing value should not be replaced
my $json1 = '{"nickname":"alice"}';
my @result1 = $jq->run_query($json1, '.nickname |
override existing value');
# --- 2. Undefined value should be replaced
my $json2 = '{}';
my @result2 = $jq->run_query($json2, '.nickname | default("unknown")');
is($result2[0], 'unknown', 'default()
ult for missing field');
# --- 3. Null value should be replaced
my $json3 = '{"nickname":null}';
my @result3 = $jq->run_query($json3, '.nickname | default("unknown")');
is($result3[0], 'unknown', 'de
ings;
use Test::More;
use JSON::PP;
use JQ::Lite;
my $json = <<'JSON';
{
"object": {
"third": 3,
"first": 1,
"second": 2
},
"array": ["a", "b", "c"]
}
JSON
my $jq = JQ::Lite->new;
# --- objects ---
my @object = $jq->run_query($json, '.object | keys_unsorted');
my @sorted = $jq->run_query($json, '.object | keys');
# Set equality: keys_unsorted (when sorted) should equal keys
i
= $jq->run_query($json, '.array | keys_unsorted');
is_deeply($array[0], [0, 1, 2], 'keys_unsorted returns indexes for arrays');
# --- scalars ---
my @scalar = $jq->run_query($json, '.array[0] | keys
ided as JSON command line arguments
=pod
=head1 NAME
Run::WeeklyChallenge - facilitates running a solution to https://theweeklychallenge.org using one or more sets of inputs provided as JSON comman
# are given
my $inputs_example = '{"ints":[1,2,3]}';
# jsonschema (draft2020-12) for a set of inputs
my $inputs_schema_json = '{
"type": "object",
"properties": {
ma_json);
Example output:
$ perl example.pl '{"ints":[1,2,3]}' '{"ints":[]}'
Inputs: {"ints":[1,2,3]}
Output: 6
Inputs: {"ints":[]}
Output: 0
You must provide an example JSON in
:More;
use JQ::Lite;
my $json = <<'JSON';
[
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25}
]
JSON
my $jq = JQ::Lite->new;
my @results = $jq->run_query($json, '.[] | {"name": .name}'
],
'object constructor builds hashes with selected fields',
);
@results = $jq->run_query($json, '.[] | {name: .nickname}');
is_deeply(
\@results,
[
{ name => undef },
ng values are represented as null entries in constructed objects',
);
@results = $jq->run_query($json, '.[] | {}');
is_deeply(
\@results,
[ {}, {} ],
'empty object constructors yield emp
#----------------------------------------------------------------------------
## JSON Schema Validator - ~/lib/App/jsonvalidate.pm
## Version v0.1.0
## Copyright(c) 2025 DEGUEST Pte. Ltd.
## Author: J
self.
##----------------------------------------------------------------------------
package App::jsonvalidate;
use strict;
use warnings;
use vars qw( $VERSION );
our $VERSION = 'v0.1.0';
sub init
{
__END__
=encoding utf-8
=head1 NAME
App::jsonvalidate - App harness for the jsonvalidate CLI
=head1 SYNOPSIS
Run C<jsonvalidate -h> or C<perldoc jsonvalidate> for more options.
=head1 VERSION
ore;
use JQ::Lite;
my $json = q({
"users": [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25},
{"name": "Carol", "age": 35}
],
"tags": ["perl", "json", "cli"],
"title": "j
,
"flags": [true, false, true]
});
my $jq = JQ::Lite->new;
my @object_index = $jq->run_query($json, '.users | index({"name":"Bob","age":25})');
is(scalar @object_index, 1, 'object index returns si
ray_index = $jq->run_query($json, '.tags | index("perl")');
is($array_index[0], 0, 'scalar array search returns zero-based index');
my @string_index = $jq->run_query($json, '.title | index("lite")');
'0.001';
use v5.38;
use Object::Pad;
use Mojo::UserAgent;
use Mojo::URL;
use Mojo::JSON qw(encode_json decode_json);
use Time::HiRes 'sleep';
use WebService::Akeneo::HTTPError;
class WebService::
s = { Accept=>'application/json', Authorization => 'Bearer ' . $auth->bearer };
my $tx;
if (exists $opt{ndjson}) {
my $records = $opt{ndjson} // [];
die 'ndjson must be arrayref' unless r
;
die 'ndjson array empty' unless @$records;
my $ndjson = join("", map { encode_json($_)."\n" } @$records);
$headers->{'Content-Type'} = 'application/vnd.akeneo.collection+json';
$tx =
es->code,message=>$res->message,body=>($res->body//''))->throw if $res->is_error;
my $j = $res->json; $token = $j->{access_token}; $refresh = $j->{refresh_token}//$refresh; $expires_at = time + ($j-
es->code,message=>$res->message,body=>($res->body//''))->throw if $res->is_error;
my $j = $res->json; $token = $j->{access_token}; $refresh = $j->{refresh_token}//$refresh; $expires_at = time + ($j-