Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

So I want to use Datatables.js to have a nice UI, at the back end I make a database query and produce json, but Datatables isn't happy, I think my json is to blame
my @rows = $dbh->selectall_arrayref($sql, {Slice => {}}); my $cont = {data => \@rows}; $c->render(json => $cont);
My 'json' looks like:
{"data": [[ { "UNAME": "Name Goes Here", "AGE": "Age goes here", "TITLE": "Title goes here" }, more results int he same format ]]}
Datatables says "Datatables warning: table id=example - Requested unknown parameter "UNAME" for row 0, col 0. I think my json is badly formed

Replies are listed 'Best First'.
Re: Mojo Lite - Invalid JSON
by Corion (Patriarch) on Feb 28, 2020 at 16:37 UTC

    You are passing an array with a single element, that is another array full of hashes to Datatables.js. Are you sure that this is what Datatables.js wants?

    Maybe it is happier with a simple Array-of-Hashes?

    my $rows = $dbh->selectall_arrayref($sql, {Slice => {}}); $c->render( json => { data => $rows });
      Worked well, thanks for your solution
Re: Datatables.js - Invalid JSON
by Anonymous Monk on Feb 29, 2020 at 08:29 UTC

    You alway gotta read the docs :)

    var dataSet = [ ["array", "of", "arrays" ], ["array", "of", "arrays" ] ]; $('#example').DataTable( { data: dataSet, ...

    your JSON equally needs to send an array of arrays, like a table, no hashes

A reply falls below the community's threshold of quality. You may see it by logging in.