DBI fetchall_arratref, JSON and converting numeric "sting" to"number"

S

seven.reeds

Hi,

I am pulling data from some fairly simple database tables. I have a
simple SELECT query for each table and I can do the fetchall_arrayref
just fine. I want to format the returned data into a JSON string. I
am doing this with only one problem. The data returned from the fetch
is all "string"ified. If I have a table that has a numeric "id" field
then the default JSON result has that column as a set of numbers in
double quotes.

I know that I can loop over the returned array and "add zero" to the
numeric strings to have the JSON converter (JSON::Syck) treat them as
numbers. This works but looking at the code makes me wonder if there
is a more Perlesque way to do this. I've tried using map() on the
arrayref but my understanding is too shallow to make that work yet.

Ideas?
 
S

seven.reeds

Something like
    use Scalar::Util qw/looks_like_number/;

    for (@$results) {
        $_ += 0
            if looks_like_number $_;
    }

Brilliant, thanks. Much more elegant
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top