Paul Hill
Paul Hill

Reputation: 35

Convert Perl string to json array

I am attempting to write a Perl script that will run on a Centos 6 host. This script will run a shell command that queries our AWS interface like this:

my $json = `aws ec2 describe-instances`;

This query will return a JSON object but I belive my issue is that it is actually being stored as a string in $output and not actually in a JSON object.

I am trying the following code:

# Get each instance
my $json = `aws ec2 describe-instances`;

# Decode AWS json result
my $decoded = decode_json $json;

# Find reference type
print "Reference type: " . ref $decoded,"\n";

# Loop through each EC2 instance
print $decoded -> {Reservations} . "\n";

When I execute this code I get the following message:

Reference type: HASH
ARRAY(0xa774b0)

Can anyone help me with this? I have been googling and messing with this for over a whole day now :(

Below is an example of the string that is stored in $output although in the real output there would be multiple instances:

{
    "Reservations": [
        {
            "OwnerId": "82XXXXXXX043",
            "ReservationId": "r-0XXXXXXXXXXX",
            "Groups": [],
            "Instances": [
                {
                    "Monitoring": {
                        "State": "XXXXX"
                    },
                    "PublicDnsName": "XXX",
                    "RootDeviceType": "XXXXXXX",
                    "State": {
                        "Code": XXX,
                        "Name": "stopped"
                    },
                    "EbsOptimized": XXXX,
                    "LaunchTime": "XXXXXXXXXX",
                    "PrivateIpAddress": "XXXXXXXXXX",
                    "ProductCodes": [],
                    "VpcId": "XXXXXXXXXX",
                    "StateTransitionReason": "UXXXXXXXXXX",
                    "InstanceId": "XXXXXXXXXX",
                    "ImageId": "XXXXXXXXXX",
                    "PrivateDnsName": "XXXXXXXXXX",
                    "KeyName": "XXXXXXXXXX",
                    "SecurityGroups": [
                        {
                            "GroupName": "XXXXXXXXXX",
                            "GroupId": "XXXXXXXXXX"
                        },
                        {
                            "GroupName": "XXXXXXXXXX",
                            "GroupId": "XXXXXXXXXX"
                        },
                        {
                            "GroupName": "XXXXXXXXXX",
                            "GroupId": "XXXXXXXXXX"
                        }
                    ],
                    "ClientToken": "XXXXXXXXXX",
                    "SubnetId": "XXXXXXXXXX",
                    "InstanceType": "XXXXXXXXXX",
                    "NetworkInterfaces": [
                        {
                            "Status": "XXXXXXXXXX",
                            "MacAddress": "XXXXXXXXXX",
                            "SourceDestCheck": XXXXXXXXXX,
                            "VpcId": "XXXXXXXXXX",
                            "Description": "XXXXXXXXXX",
                            "NetworkInterfaceId": "XXXXXXXXXX",
                            "PrivateIpAddresses": [
                                {
                                    "Primary": XXXXXXXXXX,
                                    "PrivateIpAddress": "XXXXXXXXXX"
                                }
                            ],
                            "Ipv6Addresses": [],
                            "Attachment": {
                                "Status": "XXXXXXXXXX",
                                "DeviceIndex": 01234,
                                "DeleteOnTermination": XXXXXXXXXX,
                                "AttachmentId": "XXXXXXXXXX",
                                "AttachTime": "XXXXXXXXXX"
                            },
                            "Groups": [
                                {
                                    "GroupName": "XXXXXXXXXX",
                                    "GroupId": "XXXXXXXXXX"
                                },
                                {
                                    "GroupName": "XXXXXXXXXX",
                                    "GroupId": "XXXXXXXXXX"
                                },
                                {
                                    "GroupName": "XXXXXXXXXX",
                                    "GroupId": "XXXXXXXXXX"
                                }
                            ],
                            "SubnetId": "XXXXXXXXXX",
                            "OwnerId": "XXXXXXXXXX",
                            "PrivateIpAddress": "XXXXXXXXXX"
                        }
                    ],
                    "SourceDestCheck": XXXXXXXXXX,
                    "Placement": {
                        "Tenancy": "XXXXXXXXXX",
                        "GroupName": "XXXXXXXXXX",
                        "AvailabilityZone": "XXXXXXXXXX"
                    },
                    "Hypervisor": "XXXXXXXXXX",
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "XXXXXXXXXX",
                            "Ebs": {
                                "Status": "XXXXXXXXXX",
                                "DeleteOnTermination": XXXXXXXXXX,
                                "VolumeId": "XXXXXXXXXX",
                                "AttachTime": "XXXXXXXXXX"
                            }
                        }
                    ],
                    "Architecture": "XXXXXXXXXX",
                    "StateReason": {
                        "Message": "XXXXXXXXXX",
                        "Code": "XXXXXXXXXX"
                    },
                    "IamInstanceProfile": {
                        "Id": "XXXXXXXXXX",
                        "Arn": "XXXXXXXXXX"
                    },
                    "RootDeviceName": "XXXXXXXXXX",
                    "VirtualizationType": "XXXXXXXXXX",
                    "Tags": [
                        {
                            "Value": "XXXXXXXXXX",
                            "Key": "XXXXXXXXXX"
                        },
                        {
                            "Value": "XXXXXXXXXX",
                            "Key": "XXXXXXXXXX"
                        },
                        {
                            "Value": "XXXXXXXXXX",
                            "Key": "XXXXXXXXXX"
                        },
                        {
                            "Value": "XXXXXXXXXX",
                            "Key": "XXXXXXXXXX"
                        },
                        {
                            "Value": "XXXXXXXXXX",
                            "Key": "XXXXXXXXXX"
                        },
                        {
                            "Value": "XXXXXXXXXX",
                            "Key": "XXXXXXXXXX"
                        }
                    ],
                    "AmiLaunchIndex": XXXXXXXXXX
                }
            ]
        }
    ]
}

Upvotes: 2

Views: 1028

Answers (2)

Dave Cross
Dave Cross

Reputation: 69314

You've successfully converted the JSON string into a Perl data structure. Looks like you're falling down because you don't know how to manipulate Perl data structures. Reading the perldsc manual page would probably help there.

I see that $decoded->{Reservations} is an array reference. So you'll be able to walk that array with code like:

foreach my $reservation (@{ $decoded->{Reservations} }) {
  # Do something useful with $reservation
}

Alternatively, you might consider using a library like Paws to handle your AWS interaction.

Upvotes: 3

Sobrique
Sobrique

Reputation: 53508

Your JSON starts with a { so the error is quite correct. You don't have an array reference, you have a hash reference.

Try print ref $decoded,"\n" and it'll tell you what type of reference you have.

Try $decoded -> {Reservations} or just print the whole thing with Data::Dumper:

use Data::Dumper;
print Dumper $decoded;

Upvotes: 3

Related Questions