有条件地解析JSON文件

时间:2018-05-04 10:13:27

标签: python json

{
    "RouteTables": [
        {
            "Associations": [], 
            "RouteTableId": "rtb-ce3c7daa", 
            "VpcId": "vpc-87cf4de3", 
            "PropagatingVgws": [], 
            "Tags": [
                {
                    "Value": "ItMgmtUsEastPublic", 
                    "Key": "Name"
                }
            ], 
            "Routes": [
                {
                    "GatewayId": "local", 
                    "DestinationCidrBlock": "192.168.254.0/23", 
                    "State": "active", 
                    "Origin": "CreateRouteTable"
                }, 
                {
                    "GatewayId": "igw-961518f3", 
                    "DestinationCidrBlock": "0.0.0.0/0", 
                    "State": "active", 
                    "Origin": "CreateRoute"
                }
            ]
        }, 
        {
            "Associations": [
                {
                    "RouteTableAssociationId": "rtbassoc-27e68942", 
                    "Main": true, 
                    "RouteTableId": "rtb-92ff64f7"
                }
            ], 
            "RouteTableId": "rtb-92ff64f7", 
            "VpcId": "vpc-b8fc75dd", 
            "PropagatingVgws": [], 
            "Tags": [], 
            "Routes": [
                {
                    "GatewayId": "local", 
                    "DestinationCidrBlock": "172.31.0.0/16", 
                    "State": "active", 
                    "Origin": "CreateRouteTable"
                }, 
                {
                    "GatewayId": "igw-27cd1542", 
                    "DestinationCidrBlock": "0.0.0.0/0", 
                    "State": "active", 
                    "Origin": "CreateRoute"
                }
            ]
        }

我希望以Tags[]中的值写入时以某种方式解析此JSON,否则为Tag打印No Tag

我有这段代码:

with open('1.json') as file:
 data = json.load(file)

 for element in data['RouteTables']:
  s=element['RouteTableId'] + ',' + element['VpcId']
  if 'Tags' in element:
   for route in element['Tags']:
    d = route['Value']
   else:
    d = 'No Tag'
  for route in element['Routes']:
     r=route['DestinationCidrBlock']
     print s+','+d+','+r

无论是否有标记,都会打印No Tag

期望的输出:

rtb-ce3c7daa,vpc-87cf4de3,ItMgmtUsEastPublic,192.168.254.0/23
rtb-ce3c7daa,vpc-87cf4de3,ItMgmtUsEastPublic,0.0.0.0/0
rtb-92ff64f7,vpc-b8fc75dd,No Tag,172.31.0.0/16
rtb-92ff64f7,vpc-b8fc75dd,No Tag,0.0.0.0/0

目前的结果:

rtb-ce3c7daa,vpc-87cf4de3,No Tag,192.168.254.0/23
rtb-ce3c7daa,vpc-87cf4de3,No Tag,0.0.0.0/0
rtb-92ff64f7,vpc-b8fc75dd,No Tag,172.31.0.0/16
rtb-92ff64f7,vpc-b8fc75dd,No Tag,0.0.0.0/0

3 个答案:

答案 0 :(得分:2)

修改了你的代码。基本上你需要为每次迭代重新初始化d。

for element in data['RouteTables']:
    s=element['RouteTableId'] + ',' + element['VpcId']
    d = 'No Tag'
    if 'Tags' in element:
        for route in element['Tags']:
            d = route['Value']
    for route in element['Routes']:
        r=route['DestinationCidrBlock']
        print s+','+d+','+r

答案 1 :(得分:0)

尝试在代码中替换此行:

if 'Tags' in element:  

通过:

if element['Tags']:

答案 2 :(得分:-3)

我认为你有“其他'”的缩进词。错误。这样你就得到了一个for ... else,而不是if ... else。