Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

problem parsing json

by diamondsandperls (Beadle)
on Sep 28, 2012 at 23:43 UTC ( #996300=perlquestion: print w/ replies, xml ) Need Help??
diamondsandperls has asked for the wisdom of the Perl Monks concerning the following question:

I am not even close on this one I have parsed only easier JSON data in the past this one has a lot more going on it seems.
Thank you to all who reply


JSON SAMPLE:

"balArray" : [ { "containerArray" : [], "name" : "user_ip" }, { "containerArray" : [ { "entryArray" : [ { "action" : "flag", "expires" : "00:00:00", "hitsSinceAdded" : 128, "name" : "/my/page", "priority" : 4, "rule" : "my_rule", "timestamp" : "21:16:46.946" } ], "name" : "192.168.1.100" }, { "entryArray" : [ { "action" : "flag", "expires" : "00:00:00", "hitsSinceAdded" : 113, "name" : "/my/page2", "priority" : 4, "rule" : "my_rule2", "timestamp" : "21:18:57.750" } ], "name" : "192.168.1.101" },
SAMPLE CODE:

#JSON my $json = new JSON; my $json_text = $json->decode($content); foreach my $json_data(@{$json_text->{entryArray}}) { print {$output_fh} " $json_data->{timestamp},"; print {$output_fh} " $json_data->{name},"; print {$output_fh} "$json_data->{rule},"; print {$output_fh} "$json_data->{priority},\n"; }

Comment on problem parsing json
Select or Download Code
Re: problem parsing json
by davido (Archbishop) on Sep 28, 2012 at 23:53 UTC

    First things first: Remove all those <br> tags from your JSON sample, and instead wrap the sample in <code> tags, just like you would wrap code. Then we'll be able to see what it really looks like.

    Second, it helps us to know what you're asking if your post shows what output you're getting, what you were expecting, and actually asks a question.


    Dave

      thanks for the posting pointers much appreciated
Re: problem parsing json
by Anonymous Monk on Sep 29, 2012 at 01:15 UTC

    JSON SAMPLE:

    That is not valid JSON, mystery solved

      the call is json are you saying the heading is not. it is just a heading
        Noting next time to use data dumper to get the json information $json_text->{balArray}1{containerArray}[0]{entryArray}[0]{timestamp}

        Here's what I get when I try to parse the JSON you posted:

        $ perl -MJSON::Tiny -MData::Dumper -n0777E '$j=JSON::Tiny->new; $o=$j- +>decode($_); say length $o ? Dumper($o) : $j->error;' diamondsandperl +s.json Malformed JSON: Expected array or object at line 0, offset 0

        If you pass malformed JSON, you won't get a desirable result. You're using JSON, so with the JSON you posted, you'll get:

        $ perl -MJSON -MData::Dumper -n0777E 'say Dumper( JSON->new->decode($_ +) );' diamondsandperls.json malformed JSON string, neither array, object, number, string or atom, +at character offset 995 (before "],\n "...") at -e line + 1, <> chunk 1.

        If that's not the error you're seeing, please post the actual JSON document you're working with. Otherwise, you're not likely to get much useful help.


        Dave

        the call is json are you saying the heading is not. it is just a heading

        JSON doesn't have calls or headings, and the data you posted isn't JSON

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://996300]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-04-16 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (413 votes), past polls