I have a JSON file and I'm trying to parse it in Perl. So far I have:
use strict;
use warnings;
use JSON;
open my $fh, "/Users/arjunnayini/Desktop/map_data.json";
my @decoded_json = @{decode_json($fh)};
But I am getting an error that I have a: "malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "GLOB(0x100804ed0)") "
I'm fairly certain that the JSON file is formatted properly, so I'm not sure where this is going wrong. Any suggestions?
Assuming your call to JSON is correct, you need to slurp the file in first:
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
my $json;
{
local $/; #enable slurp
open my $fh, "<", "/Users/arjunnayini/Desktop/map_data.json";
$json = <$fh>;
}
my @decoded_json = @{decode_json($json)};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With