Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Mojo Lite - Invalid JSON

by Anonymous Monk
on Feb 28, 2020 at 16:34 UTC ( [id://11113547]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11113547]
Approved by Marshall
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-19 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found