Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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 ( [id://11143803]=note: print w/replies, xml ) Need Help??


in reply to Re^2: 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?

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:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^4: Is there a way to make a JSON out of multiple records from MySQL?
by bartender1382 (Beadle) on May 12, 2022 at 02:43 UTC

    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.


      🦛

        Finally!

        Working backwards I went through every item, which took a while, but I finally ended up back in MySQL and here's what I've deduced:

        Not every "age" field had a value, in MySQL. Therefore when any particular record with a null "age" came up in the my $json = encode_json($arrayref, $typehint_raws) command, Perl reported the uninitialized warning, yet still produced the proper json output.

        So the solution was to make sure that there are no NULL fields in MySQL.

        Thanks for everyone's help

        Making sure to cast my votes on this thread

        Honestly, I got that. But I can't figure out which is uninitialized.

        $arrayref is an array of hashfeferences, and $typehint_raws are empty, but existing arrays, one for each record.

        And that's where's I'm stuck over what could be "uninitialized"

        Is it possible that when I print Dumper $typehint_raws that I should be getting more than just?

        .... $VAR1->[0], $VAR1->[0], $VAR1->[0], ...

        After all, each record has three fields.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-19 17:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found