Monday, 15 February 2010

manipulating a JSON to correct format in perl -



manipulating a JSON to correct format in perl -

i have json prints

{"d":{"success":true,"drivers":[{"first_name":"john","last_name":"smith"},{"first_name":"jane","last_name":"doe"}]}}

the names alter depending on found in database. need force in format each result resturned in json:

push(@$dummy_data, {'name' => 'testname', 'key' => 'somekey-1234'}); push(@$dummy_data, {'name' => 'testname2', 'key' => 'somekey-5678'});

so illustration john smith in place of testname , jane testname2 how each first , lastly name in json gets pushed in format above?

let's seek new game use strict; utilize warnings; utilize json::xs; utilize data::dumper; # creating reference void array $dummy_data = []; # creating $json string $json = '{"d":{"success":true,"drivers":[{"first_name":"john","last_name":"smith"},{"first_name":"jane","last_name":"doe"}]}}'; # converting json -> perl info construction $perl_hash = decode_json $json; # feeding $dummy_data array ref hash force @$dummy_data, { name => $perl_hash->{d}->{drivers}->[0]->{first_name}, key => $perl_hash->{d}->{drivers}->[1]->{first_name} }; # print have print dumper $dummy_data; output $var1 = [ { 'name' => 'john', 'key' => 'jane' } ];

json perl

No comments:

Post a Comment