将JSON中缺少键的null值设置为csv

时间:2019-06-18 09:51:35

标签: json python-2.7

我遇到了JSON对象,该对象在某些记录中存在键“分数”,而在某些记录中则不存在。在转换时,我需要为该特定记录设置缺少键的默认值。您能告诉我如何处理这种情况吗?

Json数据:

"members": [
        {
            "uri": "/api/storeserv/systems/1636754",
            "id": "1636754",
            "system": {
                "productFamily": "7000",
                "model": "7200",
            },
            "nodes": {
                "nodeTimeSkewSecs": 0,
                "nodeCountOffline": 0,
                "cpuMedianMax": 1,
                "cpuAvgMax": 1,
                "nodeCount": 2,
                "cageCount": 6,
                "cpuTrendUri": "/api/storeserv/perform-deltas/system/1636754"
            },
            "performance": {
                "summary": {
                    "delAckPctRollingAvg7Day": 0.000027003278148635055,
                    "portInfo": {
                        "totalServiceTimeColMillis": 2.348261360014493
                    },
                    "num16sWrites7Days": 0
                },
                "portBandwidthData": {
                    "trendUri": "/api/storeserv/perform-deltas/system/1636754",
                    "total": {
                        "serviceTimeMSMax": 10.194330215454102,
                        "dataRateKBPSAvg": 6244,
                        "iopsMax": 353,
                        "dataRateKBPSMax": 2975,
                        "iopsAvg": 371
                    }
                },
                "disk": {}
            },

            "serialNumberInserv": "1636754",
            "updated": "2019-02-03T03:48:29Z",
            "status": {
                "score": 8,
                "statusTypes": [
                    "health",
                    "serviceability"
                ]
            }

        },
        {
            "uri": "/api/storeserv/systems/1636754",
            "id": "1636754",
            "system": {
                "productFamily": "7000",

                "model": "7200",
            },
            "nodes": {
                "nodeTimeSkewSecs": 0,
                "nodeCountOffline": 0,
                "cpuMedianMax": 1,
                "cpuAvgMax": 1,
                "nodeCount": 2,
                "cageCount": 6,
                "cpuTrendUri": "/api/storeserv/perform-deltas/system/1636754"
            },
            "performance": {
                "summary": {
                    "delAckPctRollingAvg7Day": 0.000027003278148635055,
                    "portInfo": {
                        "totalServiceTimeColMillis": 2.348261360014493
                    },
                    "num16sWrites7Days": 0
                },
                "portBandwidthData": {
                    "trendUri": "/api/storeserv/perform-deltas/system/1636754",
                    "total": {
                        "serviceTimeMSMax": 10.194330215454102,
                        "dataRateKBPSAvg": 6244,
                        "iopsMax": 353,
                        "dataRateKBPSMax": 2975,
                        "iopsAvg": 371
                    }
                },
                "disk": {}
            },

            "serialNumberInserv": "1636754",
            "updated": "2019-02-03T03:48:29Z",
            "status": {
                "statusTypes": [
                    "health",
                    "serviceability"
                ]
            }   
        }]

python代码

 in_json = open(source_file , 'r')
    json_data = in_json.read()

    x = json.loads(json_data)

    f = csv.writer(open(final_csv_file , "wb+"))
    for val in x["members"]:
        '''
        self.write_debug(log_message=val["uri"])
        self.write_debug(log_message="\n")
        self.write_debug(log_message=val["id"])
        '''

        f.writerow([val["serialNumberHP"]
                       , val["status"]["score"]
                       , val["system"]["systemName"]
                       , val["serialNumberInserv"]
                       , val["status"]["scoreByCategory"]["performance"]
                       , val["status"]["scoreByCategory"]["oversubscription"]
                       ])

某些记录在状态下没有分数字段,因此我想将默认值写入文件。请告诉我如何处理丢失的密钥

0 个答案:

没有答案