0% found this document useful (0 votes)
4 views3 pages

This Python Script Reads a JSON File

This Python script reads a JSON file, extracts specific data fields, and processes them into an Excel file using pandas and json libraries. It handles both required and optional fields, ensuring that missing values are accounted for. The final structured data is saved as an Excel file named output.xlsx for further analysis.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

This Python Script Reads a JSON File

This Python script reads a JSON file, extracts specific data fields, and processes them into an Excel file using pandas and json libraries. It handles both required and optional fields, ensuring that missing values are accounted for. The final structured data is saved as an Excel file named output.xlsx for further analysis.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

This Python script reads a JSON file, extracts specific data fields, processes them, and saves them

into an Excel
file. It uses the pandas and json libraries for data handling.

Importing Required Libraries

import pandas as pd
import json

● pandas is used to manipulate tabular data and save it as an Excel file.

● json is used to parse and process JSON data.

Reading the JSON File

with open('request-responses.json', encoding='utf-8', errors='ignore') as f:


data = json.load(f)
● Loads the JSON content into a Python dictionary named data.

Initializing Variables

master_dict = {}
m_key = 1
o_dict = {}
● master_dict: Stores extracted data from each JSON entry.

● m_key: A unique counter for each extracted record.

● o_dict: Stores multiple extracted records indexed by m_key.

Iterating Over JSON Data

for key, value in data.items():


● Loops through each entry in the data dictionary.

Extracting Basic Fields

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.

● The values are inside lists of dictionaries, so [0]['value'] is used.

Handling Optional Fields


if request_body.get('merchant_suggested_asin') is not None: master_dict['merchant_suggested_asin'] =
request_body['merchant_suggested_asin'][0]['value']
Else:
master_dict['merchant_suggested_asin'] = 'None'

● Checks if the key exists; assigns 'None' if missing.

More Fields Extracted

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']

● Extracts product description and category.

● Extracts warranty description or assigns 'None' if absent.

● Extracts a key feature of the product.

Storing the Extracted Data

o_dict[m_key] = master_dict.copy()
m_key += 1
master_dict = {}

● Saves master_dict in o_dict.

● Increments the key for the next record.

Converting to a DataFrame & Saving to Excel

df = pd.DataFrame(o_dict)
transpose_df = df.transpose()
transpose_df.to_excel('output.xlsx')

● Converts o_dict into a pandas DataFrame.

● Transposes the DataFrame to have rows as records.

● Saves it as an Excel file (output.xlsx).

Summary

● Reads JSON data from request-responses.json.

● Extracts relevant fields and handles missing values.

● Stores data in a structured dictionary.


● Converts it into a Pandas DataFrame.

● Saves the data in output.xlsx.

This script ensures that extracted data is properly formatted and saved for further analysis.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy