in reply to Re^11: CGI Action call
in thread CGI Action call

JSON I cut off all the sub calls after the action updatetable_167 call.

my $stmt = "SELECT * FROM users WHERE $searchfield = ?"; warn("statement = '$stmt'"); my $sth = $dbh->prepare ($stmt) or die "Error Preparing:\n" . $stm +t . "\nDBI returned: \n", $dbh->errstr; $sth->execute($searchterm) or die "Unable to execute query: " . $s +th->errstr; my $refresult = $sth->fetchrow_hashref(); my $count = $sth->rows; warn("count = '$count'"); if ($count == 0) { warn("Failed Search: '$searchfield' equal to '$searchterm' "); exit(1); } else { warn("count = '$count'"); my $json = JSON->new; $json->canonical(1); $json = encode_json($refresult); print $json; warn("Finished print '$json_str'"); exit(0); }

output:

{"DD":"2019-01-30","DP":"2018-12-31","MD":"120.00","MJ":"2018-01-30"," +address1":"1471 Meeks Rd","address2":null,"business":"JZ Electroplati +ng","city":"Warran","comments":"This is a test entry","email":"jze@ya +hoo.com","forename":"John","id":57,"lastname":"Zinzer","password":"12 +34","phone_cell":"517-240-1004","phone_home":"517-233-4378","pin":"Jb +wmZ","position":"General Member","state":"MI","user_id":19,"username" +:"bwm19","zip":"45789-2334"}

Don;t understand why not in order after $json->canonical(1);

Replies are listed 'Best First'.
Re^13: CGI Action call
by Corion (Patriarch) on Mar 14, 2018 at 20:44 UTC

    Why do you think the keys are not in sorted order? To me, they look sorted:

    { "DD":"2019-01-30", "DP":"2018-12-31", "MD":"120.00", "MJ":"2018-01-30", "address1":"1471 Meeks Rd", "address2":null, "business":"JZ Electroplating", "city":"Warran", "comments":"This is a test entry", "email":"jze@yahoo.com", "forename":"John", "id":57, "lastname":"Zinzer", "password":"1234", "phone_cell":"517-240-1004", "phone_home":"517-233-4378", "pin":"JbwmZ", "position":"General Member", "state":"MI", "user_id":19, "username":"bwm19", "zip":"45789-2334" }

    At least under the assumption that "sorted" means "sorted as sort would sort", I don't see any contradition.

      By "sorted" I mean the order the columns appear in the table

      I read your full response and understand. Is there a preference as to how the data is returned as the purpose is to load the response data back into the fields on the calling form.

      I am just trying to pull one record at a time and not a chunk of the entire table

        A hash in Perl is never sorted and doesn't have a defined sort order. If you want to impose an order on the sequence of keys in JSON output, you will need to generate the JSON output yourself. But in most cases, the order simply is not important or required.

        If you want to use the JSON to create an SQL statement to insert the data into a database again, you should list the columns to insert explicitly. For example:

        my @columns = qw(DD address ); # and the other columns, omitted for br +evity my @payload = @{ $data }{ @columns }; my $placeholders = join ",", ('?' x @columns); my $columns_str = join ",", @columns; my $sql = "insert into mytable ($columns_str) values ($placeholders);" +; my $sth = $dbh->prepare( $sql ); $sth->execute( @payload ); # this assumes you use RaiseError => 1, whi +ch you always should
Re^13: CGI Action call
by poj (Abbot) on Mar 14, 2018 at 20:55 UTC

    I think you are confusing the OO and Functional interfaces. Use either OO

    my $json = JSON->new; $json->canonical(1); my $json_str = $json->encode($hashref); print $json_str;

    or Functional

    my $json_str = to_json($hashref, {canonical => 1});
    poj