Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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


"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" : "" }, { "entryArray" : [ { "action" : "flag", "expires" : "00:00:00", "hitsSinceAdded" : 113, "name" : "/my/page2", "priority" : 4, "rule" : "my_rule2", "timestamp" : "21:18:57.750" } ], "name" : "" },

#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"; }

Replies are listed 'Best First'.
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.


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


    That is not valid JSON, mystery solved

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

        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.


        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

        Noting next time to use data dumper to get the json information $json_text->{balArray}1{containerArray}[0]{entryArray}[0]{timestamp}

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://996300]
Front-paged by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2018-07-15 23:28 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (328 votes). Check out past polls.