Skip to content

Instantly share code, notes, and snippets.

@AirstarsAsia
Created May 16, 2020 02:03
Show Gist options
  • Save AirstarsAsia/ce6f5b8fab6292df86362626986eaa50 to your computer and use it in GitHub Desktop.
Save AirstarsAsia/ce6f5b8fab6292df86362626986eaa50 to your computer and use it in GitHub Desktop.
Quick script to read in a json file and export to CSV using Pandas.
from pandas import json_normalize
from pathlib import Path
import json
# place the .json file in the same dir as this script
p = Path(r'products.json')
# read json
with p.open('r', encoding='utf-8') as f:
data = json.loads(f.read())
# Create the pandas data frame, dig in to the "_embedded key" this contains the products.
df = json_normalize(data['_embedded'], record_path='products')
# render the data frame to a CSV file
df.to_csv('results.csv', index=False, encoding='utf-8')
# See the pandas data frame
print(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment