Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7775915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:44:42+00:00 2026-06-01T17:44:42+00:00

I am trying to output some data from Perl to JSON. I can do

  • 0

I am trying to output some data from Perl to JSON. I can do a simple output, but would like to structure it better.

I have an array with an id, a start time and an end time. This is the code I am using to output:

print header('application/json');
my $json->{$entry} = \@array;
my $json_text = to_json($json);
print $json_text;

Which returns:

{"Season":[["1","1330065300","1344038401"],["7","1298505601","1312416001"]]}

But I would like to output something more like:

{"Season":0[{"id":1,"DateStart":1330065300,"DateEnd":1344038401},{"id":7,"DateStart":1298505601,"DateEnd":1312416001}]}

Can anyone help on how to better structure my output?

—UPDATE——

Thanks Michael. I have tried to implement your example.

This is the code at the moment:

foreach my $key (keys %$seasons)
    {
        $seasons->{$key} =
        [
            map
            {
                { id=>$_[0], DateStart=>$_[1], DateEnd=>$_[2] }
            } @{$seasons->{$key}}
        ];
    }

But it returns the error (referring to the foreach line):

Not a HASH reference at line 148

$seasons is an arrayref return from a SQL fetchall_arrayref

Any clues?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:44:43+00:00Added an answer on June 1, 2026 at 5:44 pm

    You basically want to convert an array of arrays to an array of hashes, and you can do this using map. Assuming $data is your structure, this should do it:

    for my $key (keys %$data) {
        $data->{$key} = [
            map {
                { id => $_->[0], DateStart => $_->[1], DateEnd => $_->[2] }
            } @{$data->{$key}}
        ];      
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to parse some data from ifconfig output with sed, but I
I am trying to parse some output data from and PBX and I have
I am trying to output some Java objects as JSON, they have List properties
I am trying to make some data from database available using XML, I have
I'm trying to output some data from a python script to a binary file.
I'm trying to parse some JSON data from the Google AJAX Search API. I
I'm trying to cluster some data I have from the KDD 1999 cup dataset
I'm trying to use XSL-stylesheets in order to transform some generated XML-data to HTML-output.
I'm having some problems trying to get the code below to output the data
I am trying to right-align the output of some PL/SQL code, but dbms_output.put_line is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.