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


Data::Sah::Filter::perl::JSON::decode_str - Decode from JSON if string, otherwise leave as-is


This document describes version 0.007 of Data::Sah::Filter::perl::JSON::decode_str (from Perl distribution Sah-Schemas-JSON), released on 2022-11-15.


Using in Sah schema's prefilters (or postfilters) clause


Using with Data::Sah:

 use Data::Sah qw(gen_validator);
 my $schema = ["str","prefilters",[["JSON::decode_str"]]];
 my $validator = gen_validator($schema);
 if ($validator->($some_data)) { print 'Valid!' }

Using with Data::Sah:Filter directly:

 use Data::Sah::Filter qw(gen_filter);

 my $filter = gen_filter([["JSON::decode_str"]]);
 # $errmsg will be empty/undef when filtering succeeds
 my ($errmsg, $filtered_value) = $filter->($some_data);

Sample data and filtering results

 "" # INVALID (String is not a valid JSON: malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before "(end of string)") at (eval 2378) line 8. ), unchanged (Empty string is not valid JSON)
 "null" # valid, becomes undef
 "[1,2,3]" # valid, becomes [1,2,3]
 "\"foo\"" # valid, becomes "foo"
 "\"foo" # INVALID (String is not a valid JSON: unexpected end of string while parsing JSON string, at character offset 4 (before "(end of string)") at (eval 2382) line 8. ), unchanged (Invalid JSON, missing closing quote)
 {} # valid, unchanged (Not a string, so passed as-is)
 undef # valid, unchanged (Not a string, so passed as-is)


Please visit the project's homepage at


Source repository is at


perlancar <>


To contribute, you can send patches by email/via RT, or send pull requests on GitHub.

Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:

 % prove -l

If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.


This software is copyright (c) 2022, 2021 by perlancar <>.

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


Please report any bugs or feature requests on the bugtracker website

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.