Perl (Scripting) - Resolve "JSON Error Empty input parsing initial state"

by
Jeremy Canfield |
Updated: October 20 2021
| Perl (Scripting) articles
Let's say the following is returned when running your Perl script.
JSON Error: empty input parsing initial state at example.pl line 10.
This error is produced by the JSON::Parse module. Let's say you have the following. Notice in this example that the $raw_json variable contains no value (it's empty). This script will produce the error.
#!/usr/bin/perl
use strict;
use warnings;
use JSON::Parse 'parse_json';
my $raw_json = "";
my $parsed_json = parse_json($raw_json);
The resolution to this is to ensure that the variable being parsed ($raw_json in this example) contains JSON, like this.
#!/usr/bin/perl
use strict;
use warnings;
use JSON::Parse 'parse_json';
my $raw_json = '{ "foo": "bar" }';
my $parsed_json = parse_json($raw_json);
Did you find this article helpful?
If so, consider buying me a coffee over at