JSON-LINQ
view release on metacpan or search on metacpan
doc/json_linq_cheatsheet.EN.txt view on Meta::CPAN
======================================================================
JSON::LINQ Cheat Sheet [EN] English
======================================================================
[ 1. Creating Queries ]
use JSON::LINQ;
# From a JSON file (top-level array)
my $q = JSON::LINQ->FromJSON('data.json');
# From a JSONL file (one JSON object per line, streaming)
my $q = JSON::LINQ->FromJSONL('events.jsonl');
# From a JSON string
my $q = JSON::LINQ->FromJSONString('[{"id":1},{"id":2}]');
# From an LTSV file (label:value, TAB-separated, streaming)
my $q = JSON::LINQ->FromLTSV('data.ltsv');
# From a CSV file (first row = header, streaming)
my $q = JSON::LINQ->FromCSV('data.csv');
my $q = JSON::LINQ->FromCSV('data.csv', sep => "\t"); # TSV
my $q = JSON::LINQ->FromCSV('f.csv', headers=>[qw(a b c)]); # headerless
my $q = JSON::LINQ->FromCSV('f.csv', headers=>[qw(a b c)], # skip existing
skip_header=>1);
# From an in-memory array
my $q = JSON::LINQ->From(\@array);
# Empty sequence
my $q = JSON::LINQ->Empty();
# Integer sequence: 1, 2, 3, 4, 5
my $q = JSON::LINQ->Range(1, 5);
# Repeat an element N times
my $q = JSON::LINQ->Repeat("x", 3);
# Note: once consumed by a terminal method, the iterator is exhausted.
# Call FromJSON/FromJSONL/From again to re-query.
[ 2. Filtering ]
->Where(sub { $_[0]{status} eq '200' })
->Where(sub { $_[0]{score} >= 80 })
->Where(sub { defined $_[0]{email} && $_[0]{email} ne '' })
[ 3. Projection ]
# Select: transform each element
->Select(sub { { path => $_[0]{url}, code => $_[0]{status} } })
->Select(sub { $_[0]{name} })
# SelectMany: flatten nested arrays (selector must return ARRAY ref)
->SelectMany(sub { [ @{ $_[0]{tags} } ] })
[ 4. Sorting ]
# Primary key -- smart (numeric if both look like numbers)
->OrderBy(sub { $_[0]{name} })
->OrderByDescending(sub { $_[0]{score} })
# Primary key -- force numeric comparison
->OrderByNum(sub { $_[0]{price} })
->OrderByNumDescending(sub { $_[0]{amount} })
# Primary key -- force string comparison (cmp)
->OrderByStr(sub { $_[0]{code} })
->OrderByStrDescending(sub { $_[0]{name} })
# Secondary key (chain after OrderBy*)
->ThenBy(sub { $_[0]{name} }) # smart ascending
->ThenByDescending(sub { $_[0]{score} }) # smart descending
->ThenByNum(sub { $_[0]{age} }) # numeric ascending
->ThenByNumDescending(sub { $_[0]{age} }) # numeric descending
->ThenByStr(sub { $_[0]{name} }) # string ascending
->ThenByStrDescending(sub { $_[0]{name} }) # string descending
# Reverse current order
->Reverse()
( run in 0.985 second using v1.01-cache-2.11-cpan-140bd7fdf52 )