in reply to
JSON parsing issue please help
Your use of Data::Dumper to dump the data you're working with is helpful to us. It proves once and for all that the data you're dealing with isn't valid JSON.
use strict;
use warnings;
use Data::Dumper;
use File::Slurp;
use JSON::Tiny;
use feature qw/say/;
my $json = read_file(\*DATA);
my $j = JSON::Tiny->new;
my $decoded = $j->decode($json);
if( defined $decoded ) {
say Dumper $decoded;
}
else {
say $j->error;
}
__DATA__
{
"alertCounts" : [
{
"count" : 5,
"rule" : "rule1"
},
{
"count" : 16,
"rule" : "rule2"
}
],
"balArray" : [
{
"containerArray" : [],
"name" : "user_ip"
},
{
"containerArray" : [
{
"entryArray" : [
{
"action" : "flag",
"expires" : "00:00:00",
"hitsSinceAdded" : 56,
"name" : "/my/page",
"priority" : 4,
"rule" : "rule1",
"timestamp" : "15:28:40.150"
}
],
"name" : "192.168.1.100"
},
{
"entryArray" : [
{
"action" : "flag",
"expires" : "00:00:00",
"hitsSinceAdded" : 185,
"name" : "/my/page",
"priority" : 4,
"rule" : "rule2",
"timestamp" : "15:12:55.961"
}
],
"name" : "192.168.1.101"
}
...and the output...
Malformed JSON: Expected comma or right square bracket while parsing a
+rray before end of data
Or once again, since you're using JSON:
, or ] expected while parsing array, at character offset 1004 (before
+"(end of string)") at ./mytest.pl line 13.
If you have valid JSON, post your valid JSON. If you have invalid JSON, don't expect code that parses JSON to work.