Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by Corion (Patriarch)
on May 11, 2022 at 06:16 UTC ( [id://11143770]=note: print w/replies, xml ) Need Help??


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

JSON also understands arrays, so you can encode the array as JSON:

my $json = encode_json(\@rawRecords);

If you want to pass the type hints, you have to pass the type hints as an array as well:

my $typehint_person = { lastname => JSON_TYPE_STRING, firstname => JSON_TYPE_STRING, age => JSON_TYPE_INT}; my $typehint_persons = [ ($typehint_person) x @rawRecords ]; my $json = encode_json(\@rawRecords, $typehint_persons);

Replies are listed 'Best First'.
Re^2: Is there a way to make a JSON out of multiple records from MySQL?
by choroba (Cardinal) on May 11, 2022 at 12:55 UTC
    > my $typehint_persons = [ ($typehint_person) x @rawRecords ];

    For arrays of the same type, there's a function where you don't need to know the number:

    my $typehint_persons = json_type_arrayof($typehint_person);

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
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:09 UTC
    Thank you, it was the "type hints" part that was throwing me off. I will have to read about the "x" operator many times before I figure out how this works.
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

    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

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found