}\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
use strict;
use warnings;
use Test::More;
use JSON::PP;
use JQ::Lite;
my $json = q({
"title": "Hello World!",
"tags": ["perl", "json", "cli"],
"names": ["Alice", "Bob", "Alfred"],
"mixed": ["
_title = $jq->run_query($json, '.title | startswith("Hello")');
ok($starts_title[0], 'startswith() returns true for matching prefix');
my @starts_false = $jq->run_query($json, '.title | startswith("W
ends_title = $jq->run_query($json, '.title | endswith("World!")');
ok($ends_title[0], 'endswith() returns true for matching suffix');
my @ends_false = $jq->run_query($json, '.title | endswith("Hello"
::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
my ($json, $query) = @_;
return [$jq->run_query($json, $query)];
}
sub encode_json {
my ($data) = @_;
require JSON::PP;
return JSON::PP::encode_json($data);
}
my $json = <<'JSON';
{
"details": {
"dimensions": {
"width": 10,
"height": 20
}
}
}
JSON
my $res = run_query($json, 'setpath(["details", "dimensions", "depth"]; 5)');
is_deeply($res->[0], {
nam
depth => 5,
},
},
}, 'setpath() adds missing nested key');
$res = run_query($json, 'setpath(["details", "dimensions", "width"]; 42)');
is_deeply($res->[0], {
name => 'widg
se Test::More;
use JQ::Lite;
my $json = q({
"meta": {
"version": "1.0",
"author": "you"
}
});
my $jq = JQ::Lite->new;
my @result1 = $jq->run_query($json, 'select(.meta has "version")');
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
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
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'