Skip to content

Instantly share code, notes, and snippets.

@mvark
Created September 1, 2024 03:52
Show Gist options
  • Save mvark/0efb9a1ffa28caba87df7e7d24da46c1 to your computer and use it in GitHub Desktop.
Save mvark/0efb9a1ffa28caba87df7e7d24da46c1 to your computer and use it in GitHub Desktop.
Python script to convert a JSON file to CSV. This script loads JSON data from a file, converts it into a structured pandas DataFrame, and then saves that DataFrame as a CSV file. The main steps involve reading the JSON file, normalizing the JSON structure into a flat format (suitable for tabular data), and finally exporting that data into a CSV …
import json
import pandas as pd
# Load JSON data, specify the file name with JSON content
with open(r'C:\OFF\raw.json') as f:
data = json.load(f)
# Convert to DataFrame
df = pd.json_normalize(data['products'])
# Save to CSV
df.to_csv(r'C:\OFF\products.csv', index=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment