Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: JSON parsing issue please help

by diamondsandperls (Beadle)
on Sep 29, 2012 at 19:36 UTC ( #996415=note: print w/ replies, xml ) Need Help??


in reply to Re: JSON parsing issue please help
in thread JSON parsing issue please help

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)


Comment on Re^2: JSON parsing issue please help
Download Code
Re^3: JSON parsing issue please help
by davido (Archbishop) on Sep 29, 2012 at 19:53 UTC

    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!

        Yeah and? What do you want?

        I believe one replier has already helped in letting me know the elements are in hash form.

        Go to http://json.org and look at the diagrams

        If this someone has parsed this JSON with their own hand-rolled parser in Ruby, then that parser is accepting invalid JSON and should be dumped in favor of one used by other Ruby projects. If this is being parsed by a popular Ruby library, alert the maintainers of that code to the JSON spec (or the RFC) epecially this part:

        array = begin-array [ value *( value-separator value ) ] end-arr +ay
        Notice that it isn't array = begin-array [ *( value value-separator ) ] end array

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-12-26 01:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls