使用python将动态嵌套json转换为csv的通用解决方案

时间:2020-02-02 03:51:16

标签: python json csv data-conversion

我是python的新手。试图解析嵌套的json并转换为csv。尝试了其他解决方案(例如Dictwriter等)中提供的各种解决方案,但只能获取数据的最后记录。 json结构是动态的,即无法指定深度。我正在寻找一种不对任何变量进行硬编码的通用解决方案。

下面是示例数据

{
    "documentType": "Rec",
    "filters": [
        {
            "name": "pc",
            "type": "string",
            "size": 50,
            "allowedValues": null,
            "isCus": false,
            "pre": null,
            "fdn": "Unique Name",
            "scale": null
        },
        {
            "name": "Ss",
            "type": "string",
            "size": 50,
            "allowedValues": null,
            "isCus": false,
            "precision": null,
            "fdn": "Status String",
            "scale": null
        }
    ],
    "selections": [
        {
            "name": "R.notify",
            "type": "boolean",
            "size": null,
            "allowedValues": null,
            "isCus": false,
            "precision": null,
            "fdn": "Notify",
            "scale": null
        },
        {
            "name": "intit",
            "type": "string",
            "size": 255,
            "allowedValues": null,
            "isCus": false,
            "precision": null,
            "fdn": "Initialization",
            "scale": null
        }
    ]
}

预期产量

"documentType","","name","type","size","allowedValues","isCus","precision","pre","fdn","scale"
"Rec","filters","pc","string","50","null","False","","null","Unique Name","null"
"","","Ss","string","50","null","False","null","","Status String","null"
"","selections","R.notify","boolean","null","null","False","null","","Notify","null"
"","","intit","string","255","null","False","null","","Initialization","null"

0 个答案:

没有答案