The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.
#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

use JSON;
use Scalar::Util 'looks_like_number';

my $json = JSON->new->allow_nonref;

our $VERSION = '1.08'; # VERSION

{
    local $/;
    my $input = $json->decode(scalar <>);
    if (ref($input) eq 'ARRAY' && @$input > 0 && @$input <= 3 &&
            looks_like_number($input->[0]) && !ref($input->[1])) {
        $input = $input->[2];
    }
    print $json->encode($input);
}

#ABSTRACT: Strip result envelope (JSON version)
#PODNAME: strip-result-env-json

__END__

=pod

=encoding UTF-8

=head1 NAME

strip-result-env-json - Strip result envelope (JSON version)

=head1 VERSION

version 1.08

=head1 SYNOPSIS

 % cmdline-script ... --format=json | strip-result-env-json

=head1 DESCRIPTION

This is a simple utility to strip result envelope (that is, the status and
message). It will change:

 [200,"OK",{"foo":"bar"}]
 [405,"Not found"]
 [200,"OK","result",{"result_metadata1":"val"}]

to:

 {"foo":"bar"}
 null
 "result"

It accepts and outputs JSON. It passes data unchanged if input data is not
enveloped result. It dies on error.

=head1 SEE ALSO

L<Perinci::CmdLine>

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/Perinci-CmdLine>.

=head1 SOURCE

Source repository is at L<https://github.com/sharyanto/perl-Perinci-CmdLine>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Perinci-CmdLine>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHOR

Steven Haryanto <stevenharyanto@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Steven Haryanto.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut