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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.