in reply to Mojolicios - render json with field names

Instad of moving your data from an array to an array of hashes (AoH) afterwards, let DBI do the work:

my $sql = 'select product, price from table'; my $results = $dbh->selectall_arrayref($sql, { Slice => {} }); warn Dumper $results;

This will return the list as hashes just like you want to use them.

Your syntax for JSON seems somewhat off, because JSON only accepts : for objects / hashes and not for arrays I think.

Replies are listed 'Best First'.
Re^2: Mojolicios - render json with field names
by Anonymous Monk on Nov 10, 2016 at 14:49 UTC
    I'm trying to use http://easyautocomplete.com/guide and need to send it JSON data like in the example. My SQL query works ok, I just don't get how to change this into a JSON return like needed in the sample data of the jQuery plugin.

      I highly doubt that you need to send the JSON in the format you described because that format is not valid JSON.

      I suggest you reconsider my approach and let DBI process the data into a format that can be directly handed to any kind of JSON processor and will produce the appropriate JSON output.

      Update:

      Note that your proposed format does not use double quotes for the entries product and price, and that your proposed format uses square brackets []. The documentation at http://easyautocomplete.com/guide does always use quoted entries and uses objects within {}.

        my fault, your example works well, i didn't copy it right