Perl query using JSON

Perl query using JSON

dogcollarlabordogcollarlabor Posts: 6Questions: 0Answers: 0

I haven't worked with Perl or Javascript in many years so please excuse any odd programming I might have done. Most of the JS was borrowed except for the addition of grabbing the row ID and appending it to the JSON.

Here's a method for working with query data in Perl that doesn't require any modules beyond CGI and JSON. POST data is JSON with the row_id appended. The row_id is then used as a key in Perl to get the value of the decoded JSON data. Code is stripped down for clarity.

Javascript:

var editor = new $.fn.dataTable.Editor( {
    ajax: {
        url:  'cgi script url',
        type: 'POST',
        contentType: 'application/json',
        processData: false,
        beforeSend:function(  jqXHR,  settings ) {
            var data = settings.data;
            var jsonAppend = {
                row_id: Object.keys(data.data)[0]
            };
            data.jsonAppend = jsonAppend;
            settings.data = JSON.stringify( data );
        }
    },
    table: "#example",
    fields: [
        { label:"Start date:", name:"start_date" }
    ]
} );

Perl:

use CGI;
use JSON;

$query = new CGI;
$postData = $query->param('POSTDATA');

if ($postData) {

    $dataHash = decode_json($postData);

    $thisAction = $dataHash->{'action'};
    
    $thisRowId = $dataHash->{'jsonAppend'}->{'row_id'};
    
    $start_data = $dataHash->{'data'}->{$thisRowId}->{'start_date'};

}
This discussion has been closed.