How to create key value JSON response in perl script with MYSQL data -


i have perl script query , return response in json format. return json array of values.

this perl script

my $sql_query = "select * table_categories"; $statement = $db_handle->prepare ($sql_query) or die "couldn't prepare query '$sql_query': $dbi::errstr\n";   $statement->execute() or die "sql error: $dbi::errstr\n";    @loop_data = ();  while (my @data = $statement->fetchrow_array()) {     push(@loop_data, @data); }     $json; $json->{"entries"} = \@loop_data;  $json_text = to_json($json); print $json_text; 

and response this

{     "entries": [         [             "1",             "salt , sugar",             "/images/salt_sugar.png",             "7"         ],         [             "2",             "tea , coffee",             "/images/tea_and_coffee.png",             "6"         ],         [             "3",             "spice , pickles",             "/images/categories/spice_pickles.png",             "7"         ],         [             "4",             "pooja needs",             "/images/categories/pooja-needs.png",             "9"         ],         [             "5",             "dry fruits",             "/images/categories/dry_fruits.png",             "7"         ]     ] } 

but want response this:

{ "entries": [     [         "id": "1",         "name": "salt , sugar",         "image": "/images/salt_sugar.png",         "rank": "7"     ],     [         "id": "2",         "name": "tea , coffee",         "image": "/images/tea_and_coffee.png",         "rank": "6"     ] ] } 

how achieve that? changes need in perl script? please help.

if don't have need loop on every row, i'd prefer use selectall_arrayref slicing:

my $json; $sql_query = "select * table_categories"; $json->{entries} = $db_handle->selectall_arrayref( $sql_query, {slice => {} } );  $json_text = to_json($json); print $json_text; 

so keys , code more compact , clear.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -