我收到了一个要转换为CSV文件的JSON文件.

[
    {
        "id": "132465",
        "ext_numero_commande": "4500291738L",
        "ext_line_items": [
            {
                "key_0": "10",
                "key_1": "10021405 / 531.415.110",
                "key_4": "4 Pce"
            },
            {
                "key_0": "20",
                "key_1": "10021258 / 531.370.140 /  NPK-Nr. 224412",
                "key_4": "4 Pce"
            },
            {
                "key_0": "30",
                "key_1": "10020895 / 531.219.120 /  NPK-Nr. 222111",
                "key_4": "10 Pce"
            },
            {
                "key_0": "40",
                "key_1": "10028633 / 552.470.110",
                "key_4": "3 Pce"
            }
        ],
        "ext_prix": [
            {
                "key_0": "11.17"
            },
            {
                "key_0": "9.01"
            },
            {
                "key_0": "18.63"
            },
            {
                "key_0": "24.15"
            }
        ],
        "ext_tag": "Date_livraison",
        "ext_jour_livraison": "23-07-2021",
        "ext_jour_livraison_1": null
    }
]
id Ext_Numero_Commande Ext_line items1 Ext_line items 4 Ext_Prix Ext_Tag Ext_Jour_Livraison Ext_Jour_Livraison 1
132465 4500291738L 10 10021405 / 531.415.110 4 Pce 11.17 Date_livraison 23-07-2021
132465 4500291738L 20 10021258 / 531.370.140 / NPK-Nr. 224412 4 Pce 9.01 Date_livraison 23-07-2021
132465 4500291738L 30 10020895 / 531.219.120 / NPK-Nr. 222111 10 Pce 18.63 Date_livraison 23-07-2021
132465 4500291738L 40 10028633 / 552.470.110 3 Pce 24.15 Date_livraison 23-07-2021

我找到了函数pd.json_normalize.

df=pd.json_normalize(
        json_object[0],
        record_path=['ext_line_items'],
        meta=['id', 'ext_numero_commande', 'ext_tag',
            'ext_jour_livraison', 'ext_jour_livraison_1'])

我几乎得到了最终结果,我可以用相同的方法和一个连接函数添加最后一列["ext_prix"].

是自动执行的功能吗?

我使用了这个函数,但它返回了一个错误.

df=pd.json_normalize(
        json_object[0],
        record_path=['ext_line_items','ext_prix'],
        meta=['id', 'ext_numero_commande', 'ext_tag',
            'ext_jour_livraison', 'ext_jour_livraison_1'])

推荐答案

如果你的JSON数据集固定长度,你可以解决这个问题,我希望这个代码能正常工作.沿着这条路走..(JSON到词典,再到CSV使用Pandas ).

# import pandas
import pandas as pd

# Read JSON file
df = pd.read_json('C://Users//jahir//Desktop//json_file.json')

# create dictionary
create_dict = {} 

# Iteration JSON file
for index,values in df.iterrows():
    
    # for loop ext_line_items
    for ext_lines in df['ext_line_items'][index]:
        for item in ext_lines:
            column_name = 'ext_line_items_'+item
            if column_name not in create_dict:
                create_dict[column_name] =[]
            
            # four time for to create four product ext_line_items_key_0, ext_line_items_key_1, ext_line_items_key_4
            time_loop = 4
            while time_loop > 0:
                time_loop-=1
                create_dict[column_name]+=[ext_lines[item]]
    
    # for loop ext_prix dataset
    # four time for to create four product ext_prix_key_0
    time_loop = 4
    while time_loop > 0:
        time_loop-=1
        for ext_prix in df['ext_prix'][index]:
            for item in ext_prix:
                column_name = 'ext_prix_'+item
                if column_name not in create_dict:
                    create_dict[column_name] =[]
                create_dict[column_name]+=[ext_prix[item]]
    # add key in dictionary 
    for i in ['id', 'ext_numero_commande', 'ext_tag', 'ext_jour_livraison']:
        create_dict[i]=[]
        
    
    # 16 time for to create 4*4 product 'id', 'ext_numero_commande', 'ext_tag', 'ext_jour_livraison'
    total_time = 16
    while total_time > 0:
        total_time-=1
        for j in ['id', 'ext_numero_commande', 'ext_tag', 'ext_jour_livraison']:
            create_dict[j] += [df[j][index]]

# Dictionary to DataFrame
pd_dict= pd.DataFrame.from_dict(create_dict) 

# DataFrame to write csv file
write_csv_path = 'C://Users//jahir//Desktop//csv_file.csv'
pd_dict.to_csv(write_csv_path, index = False, header = True)

输出:

csv output image

Json相关问答推荐

kotlinx-serialization:如何将具有不同类型对象的JsonArray转换为同一个Class

使用单元和非单元版本反序列化Rust中的枚举,而无需编写自定义反序列化程序

如何编写MongoDB查询以返回数组数组

通过在织女星简化图上裁剪来显示文本

在深度嵌套数组中使用布尔属性的jq-select

如何在VB6中将字符串转换或解码为可读格式?

如何将属性拆分为嵌套的JSON内容?

如何使用SQLite Trigger将JSON对象数组转换为新记录?

将boost::beast::multibuffer转换为std::istream

无法在 json --data 中使用变量

将文本转换为 python 列表

SwiftUI:如何使用 0 索引数组键为 JSON 添加类型

如何将任意 json 对象发布到 webapi

如何使用 Jackson 重命名 JSON 序列化中的根键

是否可以将数据写入本地 json 文件,除了Angular 之外什么都没有?

如何向从文件中检索的 JSON 数据添加键值?

AJAX 将 JavaScript 字符串数组发布到 JsonResult 作为 List 总是返回 Null?

处理 HTTP 请求正文中的可选 JSON 字段

从调试器获取 IntelliJ Idea 中的 JSON 对象

如何从 github API 解析链接头