Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

How to read Array

by sunilgame (Initiate)
on Jul 08, 2013 at 11:47 UTC ( #1043098=perlquestion: print w/replies, xml ) Need Help??
sunilgame has asked for the wisdom of the Perl Monks concerning the following question:

How to read below array
my $json = '[{"dns_zone": {"created_at":"2013-07-08T02:21:57-05:00","i +d":752,"name":"","updated_at":"2013-07-08T02:21:57-05:0 +0","user_id":136, "records": { "SOA":[ {"dns_record":{ "expire":2419200,"hostmaster":"","id": +122329,"minimum":10800,"name":"@","primaryNs":"","refre +sh":7200,"retry":900,"serial":2013032112,"ttl":86400,"type":"SOA"} } ] } } } ]';
I need read above value
print $json->{dns_zone}->{records}->{SOA}->{dns_record}->{expire}

Awaiting feedback

Replies are listed 'Best First'.
Re: How to read Array
by rjt (Deacon) on Jul 08, 2013 at 11:58 UTC

    JSON to the rescue.

    use JSON; use Data::Dump qw/dump/; # Debugging only my $raw = <>; # Raw JSON text from your post my $decoded = decode_json $raw; say $decoded->[0]->{dns_zone}->{records} ->{SOA}->[0]->{dns_record}->{expire}; # dump $decoded; # Whole mess



    While a discussion of Unicode is beyond the scope of this post, be aware that you will probably have to deal with some kind of source encoding, like utf8, but don't count on it.

      Thanks for your feedback

      Below is my JSON Array

      {"dns_zone": {"created_at":"2013-07-08T02:21:57-05:00","id":752,"name" +:"","updated_at":"2013-07-08T02:21:57-05:00","user_id":1 +36, "records":{ "SOA":[{"dns_record":{"expire":2419200,"hostmaster":""," +id":122329,"minimum":10800,"name":"@","primaryNs":"","r +efresh":7200,"retry":900,"serial":2013032112,"ttl":86400,"type":"SOA" +}}], "NS":[{"dns_record":{"hostname":"","id":122325,"name": +"","ttl":86400,"type":"NS"}},{"dns_record":{"hostname":"","id":122326,"name":"","ttl":86400,"type" +:"NS"}},{"dns_record":{"hostname":"","id":122327,"name" +:"","ttl":86400,"type":"NS"}},{"dns_record":{"hostname": +"","id":122328,"name":"","ttl":86400,"type +":"NS"}}], "A":[{"dns_record":{"id":122332,"ip":"","name":"local","ttl":14400,"type":"A"}},{"dns_record":{"id":1223 +33,"ip":"","name":"ftp","ttl":14400,"type":"A"}},{"dns +_record":{"id":122334,"ip":"","name":"mail","ttl":14400 +,"type":"A"}},{"dns_record":{"id":122335,"ip":"","name +":"webmail","ttl":14400,"type":"A"}},{"dns_record":{"id":122337,"ip": +"","name":"autodiscover","ttl":14400,"type":"A"}}], "CNAME":[{"dns_record":{"hostname":"","id":122336,"name +":"www","ttl":14400,"type":"CNAME"}}], "MX":[{"dns_record":{"hostname":"","id":122330,"na +me":"@","priority":0,"ttl":14400,"type":"MX"}}], "TXT":[{"dns_record":{"id":122331,"name":"@","ttl":14400,"txt":"v=sp +f1 ip4: +a +mx +ip4: ?all","type":"TXT"} +}] }}}

      How to read above array need to below output

      my $json = '[{"dns_record":{"ip":"","ttl":"86400","name":" +@","hostname":"","type":"soa"}}, {"dns_record":{"ip":"","ttl":"86400","n +ame":"","hostname":"","type":"ns"}}, {"dns_record":{"ip":"","ttl":"86400","n +ame":"","hostname":"","type":"ns"}}, {"dns_record":{"ip":"","ttl":"86400","na +me":"","hostname":"","type":"a"}}, {"dns_record":{"ip":"localhost","ttl":"14400","name": +"","hostname":"","type":"a"}}, {"dns_record":{"ip":"","hostname":"ares","ttl":"14400","name":"14400","type":"mx","mx_priority":"0 +"}}, {"dns_record":{"ip":"mail","ttl":"14400","name":"1440 +0","hostname":"","type":"cname"}}, {"dns_record":{"ip":"www","ttl":"14400","name":"14400 +","hostname":"","type":"cname"}}, {"dns_record":{"ip":"ftp","ttl":"14400","name":"14400 +","hostname":"","type":"cname"}}, {"dns_record":{"txt":"spf1 ip4: +a +mx + +ip4: ?all","ttl":"14400","name":"14400","hostname":"a","type":"txt"}} ]';
      Awaiting your feedback.

        By the looks of it, you want to print out all of the dns_record entries that exist within array refs for each record type (A, NS, CNAME), and then put the resulting data back into JSON form, essentially transforming one JSON response to a different format, right?

        You should have the information to start coding. Here's a basic outline: Feed your new input into my earlier example, uncomment the dump line, take a look at the result, and use that to figure out the reference path you need to get at the desired records. Once you have that, build a new ARRAY ref and push each (group of) dns_record you obtain. Finally, feed that ARRAY ref into JSON's encode_json (utf8) or to_json (no deliberate encoding), and you'll have your required output.

        Then I'd recommend you gather as much diverse real-world input as you can, and create a suite of unit tests with Test::More, as systems integration work is fundamentally a little imprecise; robust solutions are especially important here.

Re: How to read Array
by Corion (Pope) on Jul 08, 2013 at 11:54 UTC

    You have a string, not an array.

    Have you looked at JSON / JSON::Any?

Re: How to read Array
by sundialsvc4 (Abbot) on Jul 08, 2013 at 19:54 UTC

    As others have said ... what you have is a JSON-encoded string, and so the first thing that you need to is to “decode” it into the equivalent Perl data structure.   There are two subtly-different ways to do that:   from_json() and decode_json(), as eloquently discussed in JSON.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1043098]
Approved by hdb
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2016-10-22 14:47 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (295 votes). Check out past polls.