Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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


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

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; }

Replies are listed 'Best First'.
Re^5: Is there a way to make a JSON out of multiple records from MySQL?
by hippo (Bishop) on May 12, 2022 at 10:28 UTC
    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.

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

        This means you are looking at copies of the same reference.

        Win8 Strawberry 5.8.9.5 (32) Thu 05/12/2022 17:00:05 C:\@Work\Perl\monks >perl use strict; use warnings; use Data::Dumper; my $array_ref = [ ([ qw(a b c) ]) x 4 ]; print "@$array_ref \n"; print Dumper $array_ref; ^Z ARRAY(0x983824) ARRAY(0x983824) ARRAY(0x983824) ARRAY(0x983824) $VAR1 = [ [ 'a', 'b', 'c' ], $VAR1->[0], $VAR1->[0], $VAR1->[0] ];
        A referent (a thing that can be pointed to) may have any number of references (things that point to it). There is one and only one referent.


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found