Created
November 25, 2021 01:44
-
-
Save hussnainsheikh/4859312f86dd4e656811715cab1a9587 to your computer and use it in GitHub Desktop.
Python code to converts JSON data into CSV. Please change your dict obj according to your JSON data. Keep sharing!
This file contains hidden or 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
import json | |
import csv | |
# Opening JSON file | |
f = open('productfeed.json') | |
# returns JSON object as | |
# a dictionary | |
data = json.load(f) | |
# open the file in the write mode | |
f_csv = open('./productfeed.csv', 'w') | |
# create the csv writer | |
writer = csv.writer(f_csv) | |
header = ['Product Code', 'Product Title', 'Product Description', 'Keywords', 'Item Code', 'Size', 'Size Grid', 'Gender', 'Gross Weight', 'Net Weight', 'Brand', 'Category', 'Colors', 'Material', 'EAN', 'Images'] | |
writer.writerow(header) | |
# Iterating through the json | |
# list | |
for i in data['pfcProductfeed']['productfeed']['models']: | |
product = i['model'] | |
model_code = product['modelCode'] | |
title = product['description'] | |
description = product['extDesc'] | |
keywords = product['keywords'] | |
for v in product['items']: | |
colors = [] | |
images = [] | |
variant = v['item'] | |
item_code = variant['itemCode'] | |
size = variant['size'] | |
size_grid = variant['sizeGrid'] | |
gender = variant['gender'] != None if variant['gender'] else '' | |
gross_weight = variant['grossWeightKg'] | |
net_weight = variant['nettWeightKg'] | |
brand = variant['brand'] | |
category = variant['categoryData']['catDesc'] | |
for c in variant['colors']['color']: | |
colors.append(c['colorDesc']) | |
material = variant['material'] | |
ean = variant['eanCode'] | |
for key in variant['imageData']: | |
if variant['imageData'][key] != '' and variant['imageData'][key] != None: | |
images.append("https://images.pfconcept.com/ProductImages_All/JPG/500x500/"+variant['imageData'][key]) | |
writer.writerow([model_code, title, description, keywords, item_code, size, size_grid, gender, gross_weight, net_weight, brand, category, " | ".join(colors), material, ean, " | ".join(images)]) | |
# Closing file | |
f.close() | |
f_csv.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment