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

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

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

Replies are listed 'Best First'.
Re^6: How to read Array
by sunilgame (Initiate) on Jul 15, 2013 at 07:41 UTC

    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.

      If you do not want to use my code, try (untested)

      my $json = Cpanel::JSON::Dump( map { ( { 'dns_record' => $_ } ) } @dns +_record );

        Thanks for your reply, I have tried below code

        my $json = Cpanel::JSON::Dump( map { ( { 'dns_record' => $_ } ) } @dns_record );

        But return only first record

        {"dns_record":{"ip":"@","ttl":"86400","name":"86400","hostname":"sysadmin.brainhost.com","type":"SOA"}}

        I need all records same as below

        {"dns_record":{"ip":"@","ttl":"86400","name":"86400","hostname":"sys +admin.brainhost.com","type":"SOA"}}, {"dns_record":{"ip":"@","ttl":"86400","name":"86400","hostname":"192 +.64.179.10","type":"A"}}, {"dns_record":{"ip":"localhost","ttl":"14400","name":"14400","hostna +me":"127.0.0.1","type":"A"}}, {"dns_record":{"ip":"@","ttl":"86400","name":"86400","hostname":"sac +hin.com.","type":"MX","mx_priority":"0"}}, {"dns_record":{"ip":"mail","ttl":"14400","name":"14400","hostname":" +sachin.com.","type":"CNAME"}}, {"dns_record":{"ip":"www","ttl":"14400","name":"14400","hostname":"s +achin.com.","type":"CNAME"}}, {"dns_record":{"ip":"ftp","ttl":"14400","name":"14400","hostname":"s +achin.com.","type":"CNAME"}}