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

  • Home
  • SEARCH
  • 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 6846039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:34:26+00:00 2026-05-27T00:34:26+00:00

i’m trying to create a test module to test json encoding. i am having

  • 0

i’m trying to create a test module to test json encoding. i am having issues creating variables that will output correctly with the json encode/decode. if i use just the $cat_1 in the @cats array, it will work fine. however, using both, it prints out “HASH(…” as you can see below.

use strict;
use JSON;
use Data::Dump qw( dump );

my $cat_1 = {'name' => 'cat1', 'age' => '6', 'weight' => '10 kilos', 'type' => 'siamese'};
my $cat_2 = {'name' => 'cat2', 'age' => '10', 'weight' => '13 kilos', 'type' => 'siamese'};

my @cats;
push(@cats, $cat_1);
push(@cats, $cat_2);

my $dog_1 = {'name' => 'dog1', 'age' => '7', 'weight' => '20 kilos', 'type' => 'siamese'};
my $dog_2 = {'name' => 'dog2', 'age' => '5', 'weight' => '15 kilos', 'type' => 'siamese'};

my @dogs;
push(@dogs, $dog_1);
push(@dogs, $dog_2);

my $pets = {'cats' => @cats, 'dogs' => @dogs};

my $a = { 'id' => '123',    'name' => 'Joe Smith',  'city' => "Chicago", 'pets' => $pets    };

my $json = JSON->new->allow_nonref;
my $encoded = $json->encode($a);
my $decoded = $json->decode( $encoded );

print "\ndump cat_1\n";
dump $cat_1;
print "\ndump cats\n";
dump @cats;

print "\n\nOriginal\n";
dump $a;
print "\n\n";

print "Encoded\n";
print $encoded;
print "\n\n";

print "Decoded\n";
dump $decoded;
print "\n\n";

output

dump cat_1
{ age => 10, name => "cat1", type => "siamese", weight => "10 kilos" }

dump cats
(
  { age => 10, name => "cat1", type => "siamese", weight => "10 kilos" },
  { age => 10, name => "cat2", type => "siamese", weight => "3 kilos" },
)


Original
{
  city => "Chicago",
  id => 123,
  name => "Joe Smith",
  pets => {
    "cats" => { age => 10, name => "cat1", type => "siamese", weight => "10 kilos" },
    "HASH(0x176c3170)" => "dogs",
    "HASH(0x1785f2d0)" => { age => 10, name => "dog2", type => "siamese", weight => "3 kilos" },
  },
}


Encoded
{"city":"Chicago","pets":{"HASH(0x1785f2d0)":{"weight":"3     kilos","name":"dog2","type":"siamese","age":"10"},"cats":{"weight":"10 kilos","name":"cat1","type":"siamese","age":"10"},"HASH(0x176c3170)":"dogs"},"name":"Joe Smith","id":"123"}

Decoded
{
  city => "Chicago",
  id => 123,
  name => "Joe Smith",
  pets => {
    "cats" => { age => 10, name => "cat1", type => "siamese", weight => "10 kilos" },
    "HASH(0x176c3170)" => "dogs",
    "HASH(0x1785f2d0)" => { age => 10, name => "dog2", type => "siamese", weight => "3     kilos" },
  },
}
  • 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-05-27T00:34:27+00:00Added an answer on May 27, 2026 at 12:34 am

    This line

    my $pets = {'cats' => @cats, 'dogs' => @dogs};
    

    is a red flag. It’s valid Perl, but it’s not doing what you would expect. Perl will flatten your lists in this construction, so if @cats contains ($cat_1,$cat_2) and @dogs containts ($dog_1,$dog_2), your expression is parsed as

    my $pets = { 'cats', $cat_1, $cat_2, 'dogs', $dog_1, $dog_2 };
    

    which is like

    my $pets = { 'cats' => $cat_1, $cat_2 => 'dogs', $dog_1 => $dog_2 }
    

    with the hash references $cat_2 and $dog_1 getting stringified before being used as hash keys.

    Hash values must be scalar values, not arrays. But array references are OK. Try:

    my $pets = {'cats' => \@cats, 'dogs' => \@dogs};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.