如何处理嵌套JSON重复条目并转换为指定格式CSV?
问题:将含可变长度数组及重复条目的API响应转为指定格式CSV
我正尝试将API响应转换为CSV文件,当前遇到的问题为:API响应中的files和descriptions数组长度不固定,且descriptions存在重复类型条目。
期望生成的CSV包含以下列:id、img1、img2……(所有图片链接,对应files.links.url)、Market Description、Associated Comments……(所有descriptions的type作为列名,description作为对应列的值),并将重复条目保存为新行。
曾尝试使用json_normalize,但因数组长度随机且存在重复类型,不知如何处理,恳请提供解决方案或指导。
API响应示例:
{ "data": [ { "id": "35776", "type": "ProductData", "files": [ { "id": "2894907", "type": "Image", "file_extension": "JPG", "media_content": "Photo - Primary", "generic": true, "links": [ { "url": "https://d32vzsop7y1h3k.cloudfront.net/cc3a35729a75a8510c4a0b14a7fc6fd4.JPG", "height": "1070.00", "width": "800.00", "size": "L" } ] }, { "id": "6196485", "type": "Image", "file_extension": "JPG", "media_content": "Photo - Primary", "generic": true, "links": [ { "url": "https://d32vzsop7y1h3k.cloudfront.net/b8c7fa80b5e6fc9d35e2a8c8ee59e9ef.JPG", "height": "570.00", "width": "800.00", "size": "L" } ] }, { "id": "7079630", "type": "Image", "file_extension": "JPG", "media_content": "Photo - Primary", "generic": true, "links": [ { "url": "https://d32vzsop7y1h3k.cloudfront.net/7c39c695853f4a51ff1fe957cc4b2103.JPG", "height": "533.00", "width": "800.00", "size": "L" } ] }, { "id": "13400593", "type": "Image", "file_extension": "JPG", "media_content": "Photo - Primary", "generic": true, "links": [ { "url": "https://d32vzsop7y1h3k.cloudfront.net/95f42ba54e4106298efb5e84955e91a9.JPG", "height": "800.00", "width": "800.00", "size": "L" } ] }, { "id": "23536454", "type": "Image", "file_extension": "JPG", "media_content": "Photo - Primary", "generic": true, "links": [ { "url": "https://d32vzsop7y1h3k.cloudfront.net/9ef4515f90311a25e69f87111bee03f9.JPG", "height": "800.00", "width": "800.00", "size": "L" } ] }, { "id": "7079629", "type": "Image", "file_extension": "PNG", "media_content": "Logo Image", "generic": true, "links": [ { "url": "https://d32vzsop7y1h3k.cloudfront.net/b2e60a9f192a7032e8b87b966c826187.PNG", "height": "390.00", "width": "800.00", "size": "L" } ] }, { "id": "13400592", "type": "Image", "file_extension": "JPG", "media_content": "Logo Image", "generic": true, "links": [ { "url": "https://d32vzsop7y1h3k.cloudfront.net/99b17aa8393a06019fea7b4bb1b58727.JPG", "height": "417.00", "width": "800.00", "size": "L" } ] } ], "descriptions": [ { "type": "Market Description", "description": "All EBC rotors are made from OE spec grey cast iron, exactly as used on new car production and as many as we have tools for are BRITISH MADE wholly in our own UK foundry. Currently EVERY SINGLE ROTOR sold in Europe and the UK is BRITISH MADE and a majority of the designs specific to the USA are also BRITISH made.The British foundry is constantly expanding its in house tooled range but EBC like every brake manufacturer ....does need occasionally to buy some rotors in from approved sources to complete ranges in overseas markets. None of our competitors will ever tell you where their rotors come from and we at EBC are as disappointed as the rest of the world that cost pressure means there is not ONE foundry left in the USA to supply such parts, other brake brands when asked about origin will just sidestep the question. When we do buy rotors in, they are specified, sampled, tested for element analysis, grain structure, tensile strength, hardness and optimum ductility and tested 100 piece by piece to be perfect for runout on this in house machine." }, { "type": "Associated Comments", "description": "DO NOT FIT UPGRADE PADS TO REAR OF VEHICLE UNLESS SAME UPGRADE FITTED TO FRONT, The ?Thermic Black coating? does NOT need to be removed before install. Do NOT use brake cleaner or any other method to remove Thermic Black coating from discs rotors. The Thermic black coating will be removed in the pad swept area by the brake pads with in the first 10 to 20 stops made after installation on the vehicle.The ?Thermic Black coating? is an anti corrosion coating for long resistance against rust. Attempting to remove Thermic Black Coating with brake cleaner or any other method is NOT covered under EBC warranty" }, { "type": "Associated Comments", "description": "DO NOT FIT UPGRADE PADS TO REAR OF VEHICLE UNLESS SAME UPGRADE FITTED TO FRONT, The Thermic Black coating? does NOT need to be removed before install. Do NOT use brake cleaner or any other method to remove Thermic Black coating from discs rotors. The Thermic black coating will be removed in the pad swept area by the brake pads with in the first 10 to 20 stops made after installation on the vehicle.The Thermic Black coating? is an anti corrosion coating for long resistance against rust. Attempting to remove Thermic Black Coating with brake cleaner or any other method is NOT covered under EBC warranty" }, { "type": "Product Description - Extended", "description": "EBC's GD sport rotors features wide aperture slots for cooling to reduce temperatures preventing brake fade. Dimple drilling avoids stress cracks with Thermic black finish with a sleek look whilst preventing corrosion." }, { "type": "Market Description", "description": "Feature wider slots and blind drilled dimples to avoid cracks, these degas pads and remove dirt dust and debris from braking area" }, { "type": "Product Description - Extended", "description": "EBC 3GD Series Sport Slotted Rotors; Rear; 11.1 in. Dia.; Set of Two;" }, { "type": "Product Description - Short", "description": "EBC 09-14 Acura TSX 2.4 GD Sport Rear Rotors" }, { "type": "Application Summary", "description": "2004 thru 2005 HONDA Accord Sedan 3.0 Hybrid" }, { "type": "Application Summary", "description": "2006 thru 2007 HONDA Accord Sedan 3.0 Hybrid" }, { "type": "Application Summary", "description": "2013 thru 2016 HONDA Accord Sedan 2.0 hybrid" }, { "type": "Application Summary", "description": "2016 and up HONDA Accord Sedan 3.5 Touring" }, { "type": "Application Summary", "description": "2016 and up HONDA Accord Coupe 3.5 Touring" }, { "type": "Application Summary", "description": "2016 and up HONDA Accord Sedan 2.0 hybrid" }, { "type": "Product Description - Long", "description": "GD Rotors REAR Disc Brake Rotor FMSI D537" }, { "type": "Application Summary", "description": "2007 thru 2012 HONDA Accord Sedan 2.4 EX" }, { "type": "Application Summary", "description": "2008 thru 2012 HONDA Accord Sedan 2.4 LX" }, { "type": "Application Summary", "description": "2013 thru 2015 HONDA Accord Sedan 2.4 LX" }, { "type": "Application Summary", "description": "2013 thru 2015 HONDA Accord Sedan 2.4 EX" }, { "type": "Application Summary", "description": "2016 and up HONDA Accord Sedan 2.4 Sport" }, { "type": "Application Summary", "description": "2008 thru 2012 HONDA Accord Coupe 2.4 EX" }, { "type": "Application Summary", "description": "2008 thru 2012 HONDA Accord Coupe 2.4 LX" }, { "type": "Application Summary", "description": "2013 thru 2015 HONDA Accord Coupe 2.4 LX" }, { "type": "Application Summary", "description": "2013 thru 2015 HONDA Accord Coupe 2.4 EX" }, { "type": "Application Summary", "description": "2016 and up HONDA Accord Coupe 2.4 LX-S" }, { "type": "Application Summary", "description": "2016 and up HONDA Accord Coupe 3.5 EX-L" }, { "type": "Application Summary", "description": "2016 and up HONDA Accord Sedan 2.4 EX-L" }, { "type": "Application Summary", "description": "2016 and up HONDA Accord Coupe 2.4 EX-L" }, { "type": "Application Summary", "description": "2016 and up HONDA Accord Sedan 3.5 EX-L" }, { "type": "Application Summary", "description": "2016 and up HONDA Accord Coupe 2.4 EX" }, { "type": "Application Summary", "description": "2016 and up HONDA Accord Sedan 2.4 EX" }, { "type": "Application Summary", "description": "2016 and up HONDA Accord Sedan 2.4 LX" }, { "type": "Application Summary", "description": "2008 thru 2012 HONDA Accord Sedan 3.5" }, { "type": "Application Summary", "description": "2013 thru 2015 HONDA Accord Sedan 3.5" }, { "type": "Application Summary", "description": "2008 thru 2012 HONDA Accord Coupe 3.5" }, { "type": "Application Summary", "description": "2013 thru 2015 HONDA Accord Coupe 3.5" }, { "type": "Product Description - Invoice", "description": "GD7440 REAR GD Pattern Rotors" }, { "type": "Application Summary", "description": "2009 thru 2014 ACURA TSX 2.4" }, { "type": "Application Summary", "description": "2010 thru 2014 ACURA TSX 3.5" }, { "type": "Application Summary", "description": "2015 and up HONDA HR-V 1.8" }, { "type": "Application Summary", "description": "2017 and up ACURA ILX 2.4" }, { "type": "Product Description - Long", "description": "EBC GD SPORT ROTOR KIT" }, { "type": "Product Description - Short", "description": "EBC Brakes GD Sport" }, { "type": "Application Summary", "description": "2016 ACURA ILX 2.4" }, { "type": "Product Description - Short", "description": "GD Pattern Rotors" }, { "type": "AAIA Part Type Description", "description": "Disc Brake Rotor" }, { "type": "Product Description - Long", "description": "EBC GD ROTOR KIT" } ], "relationships": { "vehicle_fitments": { "links": { "self": "/v1/items/fitment/35776" } } } } ] }
解决方案
步骤1:提取基础数据与图片链接
从每个产品中提取id,遍历files数组将图片URL整理为img1、img2等键值对,动态适配数组长度变化。
步骤2:处理重复类型的描述条目
为每个descriptions条目生成独立记录:复制产品基础信息(ID+所有图片链接),再添加当前描述的类型和内容,确保重复类型的描述成为单独行。
步骤3:合并记录并生成CSV
将所有记录转换为DataFrame,自动补全缺失列的空值,最后导出为CSV文件。
完整代码示例
import pandas as pd import json # 加载API响应数据(若直接从接口获取,替换为requests.get后的json解析结果) with open('api_response.json', 'r') as f: data = json.load(f) processed_records = [] for product in data['data']: # 提取产品ID作为基础信息 base_record = {'id': product['id']} # 提取所有图片链接,生成img1、img2...格式的列 for img_idx, file_item in enumerate(product['files'], start=1): base_record[f'img{img_idx}'] = file_item['links'][0]['url'] # 为每个描述条目生成独立记录 for desc_item in product['descriptions']: new_record = base_record.copy() new_record[desc_item['type']] = desc_item['description'] processed_records.append(new_record) # 转换为DataFrame,自动适配所有列 result_df
相关产品推荐
相关产品推荐

