5

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 {}.

1 Answer 1

10

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});
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.