in reply to Re^2: How to read Array
in thread How to read Array

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.

Replies are listed 'Best First'.
Re^4: How to read Array
by sunilgame (Initiate) on Jul 09, 2013 at 10:45 UTC

    Thanks, Yes I want all dns_record entries. In above example I need read all records for "A,NS,CNAME" records type "$json->{dns_zone}->{records}->{A}" I am trying use below script

    $data = Cpanel::JSON::Load($json); my @dns_records; @dns_records = { map { $_->{dns_zone}->{records}->{A}->{dns_record}-> +{name} => $_->{dns_zone}->{records}->{A}->{dns_record}->{name} } @{$d +ata} };

    Please guide what's wrong in my script

      You are querying the wrong level of your structure. Compare to this

      my $output = to_json [ map { @$_ } values %{ (from_json $input)->{dns_ +zone}->{records} } ];

        I am new in Perl. I have trying to below query

        my $json = Cpanel::JSON::Dump( { 'dns_records' => [ map { ( { 'dns_rec +ord' => $_ } ) } @dns_record ] } );

        After that getting below array

        [{"dns_records": [ {"dns_record":{"ip":"192.64.179.9","ttl":"86400","name +":"86400","hostname":"192.64.179.10","type":"A"}}, {"dns_record":{"ip":"192.64.179.10","ttl":"14404","nam +e":"14400","hostname":"127.0.0.1","type":"A"}}, {"dns_record":{"ip":"@","ttl":"86400","name":"86400"," +hostname":"aresstest.com","type":"MX","mx_priority":"0"}}, {"dns_record":{"ip":"mail","ttl":"14400","name":"14401 +","hostname":"aresstest.com","type":"CNAME"}}, {"dns_record":{"ip":"www","ttl":"14400","name":"14402" +,"hostname":"aresstest.com","type":"CNAME"}}, {"dns_record":{"ip":"ftp","ttl":"14400","name":"14403" +,"hostname":"aresstest.com","type":"CNAME"}}]}]

        But I need below type of array

        [{"dns_record":{"ip":"192.64.179.9","ttl":"86400","name":"86400","host +name":"192.64.179.10","type":"A"}}, {"dns_record":{"ip":"192.64.179.10","ttl":"14404","nam +e":"14400","hostname":"127.0.0.1","type":"A"}}, {"dns_record":{"ip":"@","ttl":"86400","name":"86400"," +hostname":"aresstest.com","type":"MX","mx_priority":"0"}}, {"dns_record":{"ip":"mail","ttl":"14400","name":"14401 +","hostname":"aresstest.com","type":"CNAME"}}, {"dns_record":{"ip":"www","ttl":"14400","name":"14402" +,"hostname":"aresstest.com","type":"CNAME"}}, {"dns_record":{"ip":"ftp","ttl":"14400","name":"14403" +,"hostname":"aresstest.com","type":"CNAME"}}]

        Please guide.