python - 验证 JSONLint 上的 AWS json 响应

标签 python json amazon-web-services boto3

我尝试在 JSONlint 验证器上验证来自 AWS boto3 模块的 json 响应,但收到错误。

这是我运行的代码:

import boto3
import json

ec2 = boto3.resource('ec2')

for status in ec2.meta.client.describe_instance_status()['InstanceStatuses']:
    print(status)

这是回复:

{u'InstanceId': 'i-xxxxxxxx', u'InstanceState': {u'Code': 16, u'Name': 'running'}, u'AvailabilityZone': 'us-xxx-west-1b', u'SystemStatus': {u'Status': 'ok', u'Details': [{u'Status': 'passed', u'Name': 'reachability'}]}, u'InstanceStatus': {u'Status': 'ok', u'Details': [{u'Status': 'passed', u'Name': 'reachability'}]}}

当我将该响应粘贴到 JSONLint 上时,以下是响应:

Error: Parse error on line 1:
 {  u 'InstanceId': 'i-0
----^
Expecting 'STRING', '}', got 'undefined'

我假设 AWS 返回格式正确的 JSON 响应,或者没有?

最佳答案

正如 Alex 已经提到的,您正在打印一个 Python 字典,它与 JSON 略有不同。例如,“u”前缀将以下字符串标记为 unicode。

您不必担心获得无效的 JSON 响应,因为 boto3 通常会在出现 AWS API 错误时引发异常。

关于python - 验证 JSONLint 上的 AWS json 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50438099/

相关文章:

python - 关于双循环的简单python代码

jquery - 循环通过json数组jquery

amazon-web-services - AWS API Gateway HttpAPI SAM 自定义域名错误

python - 是什么导致列表索引超出范围?

python - Outlook/Python : Open specific message at screen

python - 如何正确使用带有包含间隔的多索引的 Pandas Dataframe?

javascript - 如何计算json中的相似值对象?

javascript - 通过 Prop 传递功能后 react 和检查条件

amazon-web-services - 连接()失败(113 : Host is unreachable) while connecting to upstream nginx for aws

node.js - 在elasticbeanstalk上安装npm canvas - npm安装错误