Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Is there a way to make a JSON out of multiple records from MySQL?

by bartender1382 (Beadle)
on May 12, 2022 at 00:39 UTC ( [id://11143802]=note: print w/replies, xml ) Need Help??


in reply to Re: Is there a way to make a JSON out of multiple records from MySQL?
in thread Is there a way to make a JSON out of multiple records from MySQL?

Actually have a lot of debugging to do. As I am getting a lot of the following errors, AND in addition to what appears to be a valid JSON output

For each record, I get:

Use of uninitialized value in subroutine entry..

AND a complete JSON output

If I lacked clarity, in my call to retrieve the records I am using:

my $data = $dbHandle->fetchall_arrayref();

Then I push each hash reference onto an array

foreach my $rows (@$data) { my %GenericData; @GenericData{ @rawFields } = @$rows; push @rawRecords, \%GenericData; } return \@rawRecords; ################## # which is returned to the calling function as $data #

Then I pass the reference $data to:

makeJSONToExport($data); sub makeJSONToExport { my ($hashref) = @_; my $typehint_raw = { age => JSON_TYPE_INT, lastname => JSON_TYPE_STRING, firstname => JSON_TYPE_STRING}; my $typehint_raws = [ ($typehint_raw) x @$hashref]; my $json = encode_json($hashref, $typehint_raws); # my $json = encode_json($hashref, ); return $json; }

While I'm glad I'm getting a legit JSON, I am concerned about the error msg

Replies are listed 'Best First'.
Re^3: Is there a way to make a JSON out of multiple records from MySQL?
by AnomalousMonk (Archbishop) on May 12, 2022 at 01:11 UTC
    Use of uninitialized value in subroutine entry..

    You should see at least a line number associated with this warning (not error) message, and it should be fairly easy to zero in on the argument the subroutine is expecting and not getting.

    my $typehint_raws = [ ($typehint_raw) x  @$hashref];

    If @$hashref is indeed a hash reference, I don't see how the code runs beyond that statement.

    Win8 Strawberry 5.8.9.5 (32) Wed 05/11/2022 21:07:27 C:\@Work\Perl\monks >perl use strict; use warnings; my $hr = { qw(a b c d) }; print "got here \n"; print @$hr, "\n"; # print %$hr, "\n"; # works print "and here \n"; ^Z got here Not an ARRAY reference at - line 8.


    Give a man a fish:  <%-{-{-{-<

      To be clear,I left the line number out because it means nothing, but it is definitely on the line:

      my $json = encode_json($arrayref, $typehint_raws);

      Using print Dumper I verify that:
      *$typehint_raws becomes one empty array for each record found
      *$typehint_raw is a hash which has each column and and whether it's an int or string

      Once again, everything works, but each call to the above line returns that error.

      Here's the code I use to retrieve each record....

      $data = readRaw(table name); sub readRaw { my ($rawfilename) = @_; my $dbCommand = 'select '.join ',', @rawFields; $dbCommand .= qq^ from $rawfilename;^; $dbHandle=$db->prepare($dbCommand); $dbHandle->execute(); my $data = $dbHandle->fetchall_arrayref(); $dbHandle->finish; foreach my $rows (@$data) { my %GenericData; @GenericData{ @rawFields } = @$rows; # NOTE added push @rawRecords, \%GenericData; } return \@rawRecords; }

      So I have an array of arrays, but each array holds the reference to a hash for each record read

      And $data is passed to this function

      sub makeJSONToExport { my ($arrayref) = @_; my $typehint_raw = { age => JSON_TYPE_INT, lastname => JSON_TYPE_STRING, firstname => JSON_TYPE_STRING}; my $typehint_raws = [ ($typehint_raw) x @$arrayref]; my $json = encode_json($arrayref, $typehint_raws); return $json; }

        Once again, everything works, but each call to the above line returns that error.

        As AnomalousMonk has already pointed out, this isn't an error - it's a warning. There is a singificant difference between the two which is important to understand.

        The solution is to filter or modify your data such that you do not send uninitialized values into the subroutine.


        🦛

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11143802]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-24 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found