使用Python将复杂的嵌套JSON转换为CSV或Excel

时间:2017-11-23 06:58:20

标签: python json

我有一个嵌套的JSON,我需要转换为CSV。我最初使用简单的JSON并能够转换它。但是这有一些复杂的嵌套对象,我面临着困难。以下是JSON:

{
    "next": "https://api.pro.live/api/v1.0/publisher/pages?api_key=ujwq8aW7MOZ6y40nXhrrqW0OxnIvNJnFzpWOcJaSWHc&format=json&page_number=2",
    "next_page": 2,
    "previous": null,
    "previous_page": null,
    "count": 8888,
    "data": [{
        "id": "294225",
        "publisher_id": "here",
        "name": "Company Page",
        "publisher_page_id": "",
        "publisher_page_url": "",
        "state": "active",
        "state_description": "",
        "crawled_at": null,
        "updated_at": null,
        "location": {
            "id": "163238",
            "name": "Buffard Fabrice",
            "address": {
                "address_line1": "Allée de l'Abron",
                "address_line2": "",
                "house_number": "5",
                "city": "Gennetines",
                "zip_code": "03400",
                "sublocality": "",
                "state": "",
                "country": "FR",
                "coordinates": {
                    "latitude": 46.6376484,
                    "longitude": 3.4076838
                }
            },
            "phones": [{
                "type": "main",
                "number": "0470421022",
                "country_code": ""
            }, {
                "type": "mobile",
                "number": "0662241230",
                "country_code": ""
            }],
            "urls": [{
                "type": "website",
                "url": "http://www.fabrice-buffard.fr",
                "description": "",
                "display_url": ""
            }],
            "emails": [{
                "email": "photo@fabrice-buffard.fr",
                "type": "contact",
                "description": ""
            }],
            "closed": false,
            "opening_hours": {
                "periods": [{
                    "open_day": "MONDAY",
                    "open_time": "14:00",
                    "close_day": "MONDAY",
                    "close_time": "19:00"
                }, {
                    "open_day": "TUESDAY",
                    "open_time": "10:00",
                    "close_day": "TUESDAY",
                    "close_time": "12:30"
                }, {
                    "open_day": "TUESDAY",
                    "open_time": "14:00",
                    "close_day": "TUESDAY",
                    "close_time": "19:00"
                }, {
                    "open_day": "WEDNESDAY",
                    "open_time": "10:00",
                    "close_day": "WEDNESDAY",
                    "close_time": "12:30"
                }, {
                    "open_day": "WEDNESDAY",
                    "open_time": "14:00",
                    "close_day": "WEDNESDAY",
                    "close_time": "19:00"
                }, {
                    "open_day": "THURSDAY",
                    "open_time": "10:00",
                    "close_day": "THURSDAY",
                    "close_time": "12:30"
                }, {
                    "open_day": "THURSDAY",
                    "open_time": "14:00",
                    "close_day": "THURSDAY",
                    "close_time": "19:00"
                }, {
                    "open_day": "FRIDAY",
                    "open_time": "10:00",
                    "close_day": "FRIDAY",
                    "close_time": "12:30"
                }, {
                    "open_day": "FRIDAY",
                    "open_time": "14:00",
                    "close_day": "FRIDAY",
                    "close_time": "19:00"
                }, {
                    "open_day": "SATURDAY",
                    "open_time": "10:00",
                    "close_day": "SATURDAY",
                    "close_time": "12:30"
                }]
            },
            "specific_opening_hours": [],
            "publisher_categories": [{
                "name": "Photographer",
                "id": "gcid:photographer"
            }]
        }
    }]
}

我能够从上面的json中提取很少的对象,但是当涉及到复杂的嵌套结构时,它变得很困难。我需要帮助来开发一个I泛型转换器,这样如果一个结构错过了一个字段,那么它不应该给我一个错误。

下面是我的代码的一部分,我单独提取每个对象但在一个结构错过字段时不起作用,

x["data"][numbers]["id"],
                x["data"][numbers]["publisher_id"],
                x["data"][numbers]["name"],
                x["data"][numbers]["publisher_page_id"],
                x["data"][numbers]["publisher_page_url"],
                x["data"][numbers]["state"],
                x["data"][numbers]["state_description"],
                x["data"][numbers]["crawled_at"],
                x["data"][numbers]["updated_at"]

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

试试这个

x["data"][numbers].get("id"),
x["data"][numbers].get("publisher_id"),
...
如果dict中没有键,

.get(...)将返回None。如果要返回其他默认值,请使用.get(key,default)