Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign a name to JSON Array in Perl

Tags:

json

perl

In my script to_json() converting loop_data in json format and assigning to $json.

my $json = to_json(\@loop_data);
print $json;

gives this output.

[
    {
        "Name": "Vivek",
        "Age": 20
    },
    {
        "Name": "Sonali",
        "Age": 19
    }
]

But instead of that i want print $json to give this output.

{"Friends" : [
    {
        "Name": "Vivek",
        "Age": 20
    },
    {
        "Name": "Sonali",
        "Age": 19
    }
]}

Where to add "Friends" and {}.

like image 238
Devesh Agrawal Avatar asked Jun 30 '15 14:06

Devesh Agrawal


1 Answers

You could pass a reference to a hash with Friends as the key and a reference to your array as the value:

my $json = to_json({"Friends" => \@loop_data});
like image 61
toolic Avatar answered Nov 05 '22 20:11

toolic