Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

by bartender1382 (Beadle)
on May 11, 2022 at 02:57 UTC ( [id://11143765]=perlquestion: print w/replies, xml ) Need Help??

bartender1382 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to transfer data from MySQL, over to an app in Swift.

I use the following code when I have to send over just ONE record:

This creates a hash of every field in my table

sub reader { foreach my $rows (@$data) { my %GenericData; @GenericData{ @rawFields } = @$rows; # NOTE added push @rawRecords, \%GenericData; } return \@rawRecords;

Then I encode the JSON like so, so I can send it over as HTTP DATA

my $rows = reader($mysqlFilename); ...in reader my $json = encode_json($hashref, { lastname => JSON_TYPE_STRING, firstname => JSON_TYPE_STRING, age => JSON_TYPE_INT} ); ...and I return $json which is the single record.

However, I am now pulling more than one record, and I wondered if it's possible to export multiple records to JSON?

Replies are listed 'Best First'.
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

    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);
      > 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]
      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.

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

Log In?
Username:
Password:

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

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

    No recent polls found