This Python Script Reads a JSON File
This Python Script Reads a JSON File
into an Excel
file. It uses the pandas and json libraries for data handling.
import pandas as pd
import json
Initializing Variables
master_dict = {}
m_key = 1
o_dict = {}
● master_dict: Stores extracted data from each JSON entry.
master_dict['sku'] = value['sku']
master_dict['seller_id'] = value['seller_id']
request_body = json.loads(value['request']['body'])
master_dict['item_name'] = request_body['item_name'][0]['value']
master_dict['brand'] = request_body['brand'][0]['value']
● Extracts SKU , Seller ID., Item Name and Brand.
● Converts the JSON string inside the request body into a Python dictionary.
master_dict['rtip_product_description'] = request_body.get('rtip_product_description')[0]['value']
master_dict['product_category'] = request_body.get('product_category')[0]['value']
if request_body.get('warranty_description') is not None:
master_dict['warranty_description'] = request_body['warranty_description'][0]['value']
else:
master_dict['warranty_description'] = 'None'
master_dict['bullet_point'] = request_body.get('bullet_point')[0]['value']
o_dict[m_key] = master_dict.copy()
m_key += 1
master_dict = {}
df = pd.DataFrame(o_dict)
transpose_df = df.transpose()
transpose_df.to_excel('output.xlsx')
Summary
This script ensures that extracted data is properly formatted and saved for further analysis.