将Perl字符串转换为json数组

时间:2018-04-25 14:20:42

标签: json string perl

我正在尝试编写一个将在Centos 6主机上运行的Perl脚本。该脚本将运行一个shell命令,用于查询我们的AWS界面,如下所示:

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

此查询将返回一个JSON对象,但我相信我的问题是它实际上是作为字符串存储在$ output中而不是实际存储在JSON对象中。

我正在尝试以下代码:

# 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";

当我执行此代码时,我收到以下消息:

Reference type: HASH
ARRAY(0xa774b0)

任何人都可以帮我吗?我一直在谷歌上搜索和搞乱这一整天:(

下面是存储在$ output中的字符串示例,尽管在实际输出中会有多个实例:

{
    "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
                }
            ]
        }
    ]
}

2 个答案:

答案 0 :(得分:3)

您的JSON以{开头,因此错误非常正确。你没有数组引用,你有一个哈希引用。

尝试print ref $decoded,"\n",它会告诉您您的参考类型。

尝试$decoded -> {Reservations}或只使用Data::Dumper打印整个内容:

use Data::Dumper;
print Dumper $decoded;

答案 1 :(得分:3)

您已成功将JSON字符串转换为Perl数据结构。看起来你摔倒了,因为你不知道如何操纵Perl数据结构。阅读perldsc手册页可能对此有所帮助。

我看到$decoded->{Reservations}是一个数组引用。因此,您可以使用以下代码遍历该数组:

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

或者,您可以考虑使用Paws之类的库来处理您的AWS互动。