Created
May 16, 2020 02:03
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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