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

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.


🦛

  • Comment on Re^5: Is there a way to make a JSON out of multiple records from MySQL?

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

    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

Re^6: Is there a way to make a JSON out of multiple records from MySQL?
by bartender1382 (Beadle) on May 12, 2022 at 18:44 UTC

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

        I already solved my problem, posted about it. But wanted to further explain what you pointed out.

        I don't believe that I am looking at copies of the same reference

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

        I'm looking at $typehint_raws just before calling the encode_json command. So I believe I am seeing it just after it's initialized for the proper size, but before it's filled in by encode_json