首页 > 解决方案 > 在响应对象中序列化日期时间对象(Python Rest API)

问题描述

我想在 Lambda 中序列化以下数据并返回它。这里的问题是这个 JSON 有一个 datetime 对象。有人可以帮我序列化这个 JSON,以便我可以将它返回给我的 REST API。

注意:这是来自 boto3.client.describe_instances 的完整响应(针对一个实例过滤)

{
  'Reservations': [
    {
      'Groups': [
        
      ],
      'Instances': [
        {
          'AmiLaunchIndex': 0,
          'ImageId': '<ami>',
          'InstanceId': '<instance_id>',
          'InstanceType': 't2.micro',
          'KeyName': '<MyKeyPair>',
          'LaunchTime': datetime.datetime(2021,
          4,
          25,
          15,
          41,
          54,
          tzinfo=tzlocal()),
          'Monitoring': {
            'State': 'disabled'
          },
          'Placement': {
            'AvailabilityZone': 'ap-south-1a',
            'GroupName': '',
            'Tenancy': 'default'
          },
          'PrivateDnsName': 'ip-172-31-44-178.ap-south-1.compute.internal',
          'PrivateIpAddress': '172.31.44.178',
          'ProductCodes': [
            
          ],
          'PublicDnsName': 'ec2-13-234-232-68.ap-south-1.compute.amazonaws.com',
          'PublicIpAddress': '13.234.232.68',
          'State': {
            'Code': 16,
            'Name': 'running'
          },
          'StateTransitionReason': '',
          'SubnetId': 'subnet-cb5bb8a0',
          'VpcId': 'vpc-6cbf7a07',
          'Architecture': 'x86_64',
          'BlockDeviceMappings': [
            {
              'DeviceName': '/dev/sda1',
              'Ebs': {
                'AttachTime': datetime.datetime(2021,
                4,
                25,
                15,
                41,
                54,
                tzinfo=tzlocal()),
                'DeleteOnTermination': True,
                'Status': 'attached',
                'VolumeId': 'vol-0a7f3ae04c35a5b09'
              }
            }
          ],
          'ClientToken': '20d1a714-5f5c-46aa-b4c7-65a917bcea4a',
          'EbsOptimized': False,
          'EnaSupport': True,
          'Hypervisor': 'xen',
          'NetworkInterfaces': [
            {
              'Association': {
                'IpOwnerId': 'amazon',
                'PublicDnsName': 'ec2-13-234-232-68.ap-south-1.compute.amazonaws.com',
                'PublicIp': '13.234.232.68'
              },
              'Attachment': {
                'AttachTime': datetime.datetime(2021,
                4,
                25,
                15,
                41,
                54,
                tzinfo=tzlocal()),
                'AttachmentId': 'eni-attach-007365005fa2592f2',
                'DeleteOnTermination': True,
                'DeviceIndex': 0,
                'Status': 'attached',
                'NetworkCardIndex': 0
              },
              'Description': '',
              'Groups': [
                {
                  'GroupName': 'default',
                  'GroupId': 'sg-d23193a8'
                }
              ],
              'Ipv6Addresses': [
                
              ],
              'MacAddress': '02:ee:8b:8d:8a:a2',
              'NetworkInterfaceId': 'eni-05bf048dfa0537c09',
              'OwnerId': '372401663154',
              'PrivateDnsName': 'ip-172-31-44-178.ap-south-1.compute.internal',
              'PrivateIpAddress': '172.31.44.178',
              'PrivateIpAddresses': [
                {
                  'Association': {
                    'IpOwnerId': 'amazon',
                    'PublicDnsName': 'ec2-13-234-232-68.ap-south-1.compute.amazonaws.com',
                    'PublicIp': '13.234.232.68'
                  },
                  'Primary': True,
                  'PrivateDnsName': 'ip-172-31-44-178.ap-south-1.compute.internal',
                  'PrivateIpAddress': '172.31.44.178'
                }
              ],
              'SourceDestCheck': True,
              'Status': 'in-use',
              'SubnetId': 'subnet-cb5bb8a0',
              'VpcId': 'vpc-6cbf7a07',
              'InterfaceType': 'interface'
            }
          ],
          'RootDeviceName': '/dev/sda1',
          'RootDeviceType': 'ebs',
          'SecurityGroups': [
            {
              'GroupName': 'default',
              'GroupId': 'sg-d23193a8'
            }
          ],
          'SourceDestCheck': True,
          'VirtualizationType': 'hvm',
          'CpuOptions': {
            'CoreCount': 1,
            'ThreadsPerCore': 1
          },
          'CapacityReservationSpecification': {
            'CapacityReservationPreference': 'open'
          },
          'HibernationOptions': {
            'Configured': False
          },
          'MetadataOptions': {
            'State': 'applied',
            'HttpTokens': 'optional',
            'HttpPutResponseHopLimit': 1,
            'HttpEndpoint': 'enabled'
          },
          'EnclaveOptions': {
            'Enabled': False
          }
        }
      ],
      'OwnerId': '372401663154',
      'ReservationId': 'r-0da3a940be66fbf58'
    }
  ],
  'ResponseMetadata': {
    'RequestId': '8582a3a7-93d2-467f-a3c9-e810c9c356e1',
    'HTTPStatusCode': 200,
    'HTTPHeaders': {
      'x-amzn-requestid': '8582a3a7-93d2-467f-a3c9-e810c9c356e1',
      'cache-control': 'no-cache, no-store',
      'strict-transport-security': 'max-age=31536000; includeSubDomains',
      'content-type': 'text/xml;charset=UTF-8',
      'content-length': '7122',
      'vary': 'accept-encoding',
      'date': 'Sun, 25 Apr 2021 15:42:19 GMT',
      'server': 'AmazonEC2'
    },
    'RetryAttempts': 0
  }
}

标签: jsonpython-3.xamazon-ec2serializationaws-lambda

解决方案


推荐阅读