Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: JSON parsing issue please help

by davido (Archbishop)
on Sep 29, 2012 at 18:45 UTC ( #996413=note: print w/ replies, xml ) Need Help??


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.


Dave


Comment on Re: JSON parsing issue please help
Select or Download Code
Re^2: JSON parsing issue please help
by diamondsandperls (Beadle) on Sep 29, 2012 at 19:36 UTC
    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)

      No, you're not getting the output you show using the JSON you posted. Look at the #@%!^ JSON you posted. It is not capable of resulting in the output you are showing. Here's what I get when I test it:

      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.


      Dave

        The thing im trying to parse on my end is JSON for 120% sure bc someone has parsed this with Ruby and I have seen their code; however, I want to parse this with perl just for the sake of learning.

        Rather than getting frustrated and helping just disregard. I believe one replier has already helped in letting me know the elements are in hash form.

        Has to be a typo on my end the json i posted is a snapshot of the entire file which I CAN NOT POST bc there is sensitive data in the file and the file is HUGE so posting the entire thing anyway just isn't plausible because I would have to edit the !@#$!@#$ thing!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://996413]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (14)
As of 2014-10-21 12:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (103 votes), past polls