diamondsandperls has asked for the wisdom of the Perl Monks concerning the following question:
The below json call is really what i am trying to parse.
$VAR1 = '{
"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"
}
'
;
SAMPLE CODE:
#JSON
my $json = new JSON;
my $json_text = $json->decode($content);
foreach my $json_data(@{$json_text->{balArray}[0]{containerArray}[0]{e
+ntryArray}[0]}) {
print {$output_fh} "$json_data->{timestamp},";
print {$output_fh} "$json_data->{flag},\n";
}
Re: JSON parsing issue please help
by BrowserUk (Patriarch) on Sep 29, 2012 at 18:14 UTC
|
It is usual to state what problem you are having, or how the results of your code differ from your expectations.
However, looking at your code, I see you are trying to iterate the array held in the first element of the entryArray array:
@{ $json_text->{balArray}[0]{containerArray}[0]{entryArray}[0] }
...........................................................^^^
But looking at the json, the elements of the entryArray array, do not contain arrays, they contain hashes.
If you were using strict and warnings, you'd be getting a "Not an ARRAY reference at .." message pointing at the above line, which would be a strong clue to the solution of your problem.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP Neil Armstrong
/font | [reply] [d/l] [select] |
|
If you were using strict and warnings, you'd be getting a "Not an ARRAY reference at .." message pointing at the above line, which would be a strong clue to the solution of your problem.
neither strict nor warnings are required
$ perl -e " $f = { }; $f->[0]; "
Not an ARRAY reference at -e line 1.
| [reply] [d/l] |
|
Good point.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP Neil Armstrong
| [reply] |
Re: JSON parsing issue please help
by davido (Cardinal) on Sep 29, 2012 at 18:45 UTC
|
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.
| [reply] [d/l] [select] |
|
I change the code some see below.
#JSON
my $json = new JSON;
my %json_text = %{ $json->decode($content) };
while (my ($key, $value) = each %json_text) {
print "$key = $value\n";
}
close $output_fh;
OUTPUT I AM GETTING:
alertCounts = ARRAY(0x32dadd4)
balArray = ARRAY(0x34a7e94)
| [reply] [d/l] |
|
use strict;
use warnings;
use File::Slurp;
use JSON;
my $json = read_file(\*DATA);
my $j = JSON->new;
my %json_text = %{ $j->decode($json) };
while( my ( $key, $value ) = each %json_text ) {
print "$key = $value\n";
}
__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:
, or ] expected while parsing array, at character offset 1206 (before
+"(end of string)") at ./mytest.pl line 12.
I don't know how many more times you need someone to tell you that the sample input you're posting isn't capable of producing any reasonable output because it's broken. Post real JSON that isn't malformed, and we can begin to talk about your code.
| [reply] [d/l] [select] |
|
|
|
|
A reply falls below the community's threshold of quality. You may see it by logging in. |
|
|