The Perl and Raku Conference 2025: Greenville, South Carolina - June 27-29 Learn more

NAME

JSON::Lines - Parse JSONLines with perl.

VERSION

Version 1.03

SYNOPSIS

Quick summary of what the module does.

my $jsonl = JSON::Lines->new();
my @data = (
["Name", "Session", "Score", "Completed"],
["Gilbert", "2013", 24, true],
["Alexa", "2013", 29, true],
["May", "2012B", 14, false],
["Deloise", "2012A", 19, true]
);
my $string = $jsonl->encode(@data);
my $file = $jsonl->encode_file('score.jsonl', @data);
# ["Name", "Session", "Score", "Completed"]
# ["Gilbert", "2013", 24, true]
# ["Alexa", "2013", 29, true]
# ["May", "2012B", 14, false]
# ["Deloise", "2012A", 19, true]
...
my $all = $jsonl->decode_file($file);
open my $fh, '<', $file or die $!;
while (my $line = $jsonl->get_line($fh)) {
push @lines, $line;
}
close $fh;
open my $fh, '<', $file or die $!;
my @hundred_lines = $jsonl->get_lines($fh, 100);
close $fh;
...
use JSON::Lines qw/jsonl/;
my $data = [
{
"name" => "Gilbert",
"wins" => [
["straight", "7♣"],
["one pair", "10♥"]
]
},
{
"name" => "Alexa",
"wins" => [
["two pair", "4♠"],
["two pair", "9♠"]
]
},
{
"name" => "May",
"wins" => []
},
{
"name" => "Deloise",
"wins" => [
["three of a kind", "5♣"]
]
}
];
my $string = jsonl( canonical => 1, encode => 1, data => $data );
# {"name": "Gilbert", "wins": [["straight", "7♣"], ["one pair", "10♥"]]}
# {"name": "Alexa", "wins": [["two pair", "4♠"], ["two pair", "9♠"]]}
# {"name": "May", "wins": []}
# {"name": "Deloise", "wins": [["three of a kind", "5♣"]]}

DESCRIPTION

JSON Lines is a convenient format for storing structured data that may be processed one record at a time. It works well with unix-style text processing tools and shell pipelines. It's a great format for log files. It's also a flexible format for passing messages between cooperating processes.

https://jsonlines.org/

EXPORT

jsonl

my $string = jsonl( success_cb => sub { ... }, error_cb => sub { ... }, encode => 1, data => $aoh );
my $aoa = jsonl( parse_headers => 1, decode => 1, data => $string )

SUBROUTINES/METHODS

new

Instantiate a new JSON::Lines object.

my $jsonl = JSON::Lines->new(
success_cb => sub { if ($_[0] eq 'encode') { ... } },
error_cb => sub { if ($_[0] eq 'decode') { ... } },
canonical => 1,
utf8 => 1,
pretty => 1,
parse_headers => 1
);

success_cb

Callback called on successfull encode or decode of an item.

error_cb

Callback called on unsucessfull encode or decode of an item.

canonical

Print in canonical order.

utf8

utf8 encode/decode

pretty

Pretty print.

parse_headers

Parse the first line of the stream as headers.

encode

Encode a perl struct into a json lines string.

$jsonl->encode( $data );

encode_file

Encode a perl struct into a json lines file.

$jsonl->encode_file($file, $data);

decode

Decode a json lines string into a perl struct.

$jsonl->decode( $string );

decode_file

Decode a json lines file into a perl struct.

$jsonl->decode_file( $file );

add_line

Add a new line to the current JSON::Lines stream.

$jsonl->add_line($line);
$jsonl->add_line($line, $fh);

get_line

Decode a json lines file, one line at a time.

open my $fh, "<", $file or die "$file: $!";
my $line = $jsonl->get_line($fh);
close $fh;

get_lines

Decode a json lines file, 'n' lines at a time.

open my $fh, "<", $file or die "$file: $!";
my @lines = $jsonl->get_lines($fh, 100);
close $fh;

clear_stream

Clear the current JSON::Lines stream.

$jsonl->clear_stream;

AUTHOR

LNATION, <thisusedtobeanemail at gmail.com>

BUGS

Please report any bugs or feature requests to bug-json-lines at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=JSON-Lines. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc JSON::Lines

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

This software is Copyright (c) 2020->2021 by LNATION.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)

1 POD Error

The following errors were encountered while parsing the POD:

Around line 237:

Non-ASCII character seen before =encoding in '"7♣"],'. Assuming UTF-8