Python 使用Pandas将嵌套的JSON转换为CSV,然后将CSV转换回JSON

Python 使用Pandas将嵌套的JSON转换为CSV,然后将CSV转换回JSON,python,json,python-3.x,pandas,dataframe,Python,Json,Python 3.x,Pandas,Dataframe,我有一个JSON主体,需要转换为CSV。然后,我需要从CSV进行完美的转换,以输出相同的JSON。因此,首先是JSON到CSV,然后是CSV到JSON 以下是JSON: { "quoteId": "", "carrier": "", "productType": "", "effectiveDate": ""

我有一个JSON主体,需要转换为CSV。然后,我需要从CSV进行完美的转换,以输出相同的JSON。因此,首先是JSON到CSV,然后是CSV到JSON

以下是JSON:

{
    "quoteId": "",
    "carrier": "",
    "productType": "",
    "effectiveDate": "",
    "referenceId": "",
    "agencyLocationCode": null,
    "locationId": null,
    "property": {
        "id": "",
        "address": {
            "propertyStreetNumber": "",
            "propertyStreetName": "",
            "propertyAddressLine2": null,
            "propertyCity": "",
            "propertyState": "",
            "propertyZipCode": ""
        },
        "details": null
    }
}
我运行以下代码得到的输出是:

输出csv:

代码:

现在,我希望将CSV转换回完全相同的JSON,目前我运行以下代码:

df = pd.read_csv('file.csv')
df.to_json('new.json')
我得到的输出JSON如下所示:

{
    "quoteId": {
        "0": null
    },
    "carrier": {
        "0": ""
    },
    "productType": {
        "0": ""
    },
    "effectiveDate": {
        "0": ""
    },
    "referenceId": {
        "0": 62646354
    },
    "agencyLocationCode": {
        "0": null
    },
    "locationId": {
        "0": null
    },
    "losses": {
        "0": null
    },
    "property.id": {
        "0": null
    },
    "property.address.propertyStreetNumber": {
        "0": ""
    },
    "property.address.propertyStreetName": {
        "0": ""
    },
    "property.address.propertyAddressLine2": {
        "0": null
    },
    "property.address.propertyCity": {
        "0": ""
    },
    "property.address.propertyState": {
        "0": ""
    },
    "property.address.propertyZipCode": {
        "0": ""
    },
    "property.details": ""
}

我需要输出json看起来像第一个。请帮忙

这回答了你的问题吗?使用
df.to_json('new.json',orient=“records”)
可以更接近您,但我不知道要添加一个简单的参数来重新嵌套结果。我可能会在没有熊猫的情况下从csv重建json。是否需要使用熊猫?
{
    "quoteId": {
        "0": null
    },
    "carrier": {
        "0": ""
    },
    "productType": {
        "0": ""
    },
    "effectiveDate": {
        "0": ""
    },
    "referenceId": {
        "0": 62646354
    },
    "agencyLocationCode": {
        "0": null
    },
    "locationId": {
        "0": null
    },
    "losses": {
        "0": null
    },
    "property.id": {
        "0": null
    },
    "property.address.propertyStreetNumber": {
        "0": ""
    },
    "property.address.propertyStreetName": {
        "0": ""
    },
    "property.address.propertyAddressLine2": {
        "0": null
    },
    "property.address.propertyCity": {
        "0": ""
    },
    "property.address.propertyState": {
        "0": ""
    },
    "property.address.propertyZipCode": {
        "0": ""
    },
    "property.details": ""
}