Skip to content

Instantly share code, notes, and snippets.

@hussnainsheikh
Created November 25, 2021 01:44
Show Gist options
  • Save hussnainsheikh/4859312f86dd4e656811715cab1a9587 to your computer and use it in GitHub Desktop.
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!
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