【问题标题】:How to convert a JSON with array to CSV with python or R如何使用 python 或 R 将带有数组的 JSON 转换为 CSV
【发布时间】:2020-12-17 03:48:13
【问题描述】:

我正在尝试将带有数组的 JSON 转换为 CSV,但由于该数组可以包含不同的内容,所以到目前为止我还没有找到解决方案。

这是 JSON 的示例

[
{
    "name": "Doc 1",
    "description": "This is Document 1",
    "createdby": "User 1",
    "uid": "101",
    "created": "2020-01-01T12:00:00.000Z",
    "changed": "2020-01-01T13:00:00.000Z",
    "dim1": false,
    "dim2": false,
    "changedby": "User 1",
    "path": "/1/2/3"
},
{
    "name": "Doc 2",
    "description": "This is Document 2",
    "createdby": "User 1",
    "uid": "102",
    "created": "2020-01-01T12:00:00.000Z",
    "changed": "2020-01-01T13:00:00.000Z",
    "dim1": false,
    "dim2": false,
    "reference": [
        {
            "description": "Test1.csv",
            "uid": "9000.csv",
            "current": true
        }
    ],
    "changedby": "User 4",
    "path": "/1/2/4"
},
{
    "name": "Doc 3",
    "description": "This is Document 3",
    "createdby": "User 5",
    "uid": "105",
    "created": "2020-01-01T12:00:00.000Z",
    "changed": "2020-01-01T13:00:00.000Z",
    "dim1": false,
    "dim2": false,
    "reference": [
        {
            "description": "Test1.csv",
            "uid": "9000.csv",
            "current": true
        },
        {
            "description": "Test6.csv",
            "uid": "9005.csv",
            "current": true
        }
    ],
    "changedby": "User 4",
    "path": "/1/2/4"
},
{
    "name": "Doc 4",
    "description": "This is Document 4",
    "createdby": "User 2",
    "uid": "103",
    "created": "2020-01-01T12:00:00.000Z",
    "changed": "2020-01-01T13:00:00.000Z",
    "dim1": false,
    "dim2": false,
    "reference": [
        {
            "description": "Test2.sql",
            "uid": "9001.sql",
            "connection": {
                "type": "manual",
                "system": "SQL",
                "name": "Test2",
                "user": "sqlread1",
                "server": "server1.domain.com",
                "port": "1433",
                "sid": "300",
                "dim3": null
            },
            "current": false
        }
    ],
    "changedby": "User 4",
    "path": "/1/2/5"
},
{
    "name": "Doc 5",
    "description": "This is Document 5",
    "createdby": "User 3",
    "uid": "104",
    "created": "2020-01-01T12:00:00.000Z",
    "changed": "2020-01-01T13:00:00.000Z",
    "dim1": false,
    "dim2": false,
    "reference": [
        {
            "description": "Test3.sql",
            "uid": "9002.sql",
            "connection": {
                "type": "direct",
                "system": "SQL",
                "name": "Test3",
                "user": "sqlread2",
                "server": "server2.domain.com",
                "port": "1433",
                "sid": "301",
                "dim3": null
            },
            "current": false
        },
        {
            "description": "Test4.sql",
            "uid": "9003.sql",
            "connection": {
                "type": "manuel",
                "system": "SQL",
                "name": "Test4",
                "user": "sqlread3",
                "server": "server2.domain.com",
                "port": "1433",
                "sid": "302",
                "dim3": null
            },
            "current": false
        },
        {
            "description": "Test5.sql",
            "uid": "9004.sql",
            "connection": {
                "type": "direct",
                "system": "SQL",
                "name": "Test4",
                "user": "sqlread4",
                "server": "server2.domain.com",
                "port": "1433",
                "sid": "303",
                "dim3": null
            },
            "current": false
        },
        {
            "description": "Test6.csv",
            "uid": "9005.csv",
            "current": true
        }
    ],
    "changed": "User 4",
    "path": "/1/2/4"
}]

该数组在此 JSON 中称为“引用”,该数组可能不存在,存在 3 个维度或 11 个维度,每个条目具有组“连接”以及短条目和长条目的混合。

当我在 python 中使用此代码时,我可以展平 JSON,但数组将位于单个列中。

import pandas as pd
from pandas.io.json import json_normalize
df=pd.read_json ('Sample.json')
print(df)

但我想要的应该是这样的: CSV Example

对于数组中的每个附加条目,应复制该行以使整个数组内容在相应的列中,但在单独的行中。

是否有可能使脚本通用?

谢谢! 迈克

【问题讨论】:

    标签: python arrays json csv


    【解决方案1】:

    是的,一切皆有可能! :)

    您想要做的通常被称为展平。

    你可以在这里找到一个例子:https://towardsdatascience.com/flattening-json-objects-in-python-f5343c794b10

    【讨论】:

    • 他们把数组分成多列,我想要多行
    【解决方案2】:

    你也可以这样做:

    import pandas as pd
    import json
    
    with open("yourjson.json", "r") as read_file:
        a = json.load(read_file)
    df = pd.DataFrame(a)
    print(df)
    csv_data = df.to_csv('mynewjsonfile.csv', index = False)
    

    【讨论】:

    • 结果看起来和我的 json_normalize 代码一样——数组没有“爆炸”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 2019-09-07
    • 2019-04-18
    • 1970-01-01
    相关资源
    最近更新 更多