Created
October 17, 2023 15:33
-
-
Save amenabe22/013a4edb546643698f6b01ecfef19ee5 to your computer and use it in GitHub Desktop.
Parser for wp data to shopify csv format to be imported
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
import json | |
import csv | |
from pprint import pprint | |
from slugify import slugify | |
def json_to_csv(json_data, csv_file): | |
# now we will open a file for writing | |
data_file = open(csv_file, 'w') | |
# create the csv writer object | |
csv_writer = csv.writer(data_file) | |
# Counter variable used for writing | |
# headers to the CSV file | |
count = 0 | |
for emp in json_data: | |
if count == 0: | |
# Writing headers of CSV file | |
pprint(emp) | |
header = emp.keys() | |
csv_writer.writerow(header) | |
count += 1 | |
# Writing data of CSV file | |
csv_writer.writerow(emp.values()) | |
data_file.close() | |
def process(): | |
with open("ready.json") as f: | |
data = json.load(f) | |
total_data = [] | |
for dt in data[:2]: | |
images = dt["Images"].split(",") | |
sformat = { | |
"Handle": slugify(dt["Name"]), | |
"Title": "TEST: "+dt["Name"], | |
"Body (HTML)": dt["Short description"], | |
"Vendor": "", | |
"Product Category": dt["Categories"], | |
"Type": dt["Type"], | |
"Tags": dt["Tags"], | |
"Published": "TRUE", | |
"Option1 Name": "Title", | |
"Option1 Value": dt["Name"], | |
"Option2 Name": "", | |
"Option2 Value": "", | |
"Option3 Name": "", | |
"Option3 Value": "", | |
"Variant SKU": "", | |
"Variant Grams": "", | |
"Variant Inventory Tracker": "", | |
"Variant Inventory Qty": "", | |
"Variant Inventory Policy": "deny", | |
"Variant Fulfillment Service": "manual", | |
"Variant Price": dt["Regular price"], | |
"Variant Compare At Price": "", | |
"Variant Requires Shipping": "TRUE", | |
"Variant Taxable": "TRUE", | |
"Variant Barcode": "", | |
"Image Src": images[0], | |
"Image Position": "1", | |
"Image Alt Text": "", | |
"Gift Card": "FALSE", | |
"SEO Title": dt["Name"], | |
"SEO Description": "A great description of your products in 320 characters or less", | |
"Google Shopping / Google Product Category": "Apparel & Accessories > Clothing", | |
"Google Shopping / Gender": "Unisex", | |
"Google Shopping / Age Group": "Adult", | |
"Google Shopping / MPN": "7X8ABC910", | |
"Google Shopping / AdWords Grouping": "T-shirts", | |
"Google Shopping / AdWords Labels": "cotton, pre-shrunk", | |
"Google Shopping / Condition": "used", | |
"Google Shopping / Custom Product": "FALSE", | |
"Google Shopping / Custom Label 0": "", | |
"Google Shopping / Custom Label 1": "", | |
"Google Shopping / Custom Label 2": "", | |
"Google Shopping / Custom Label 3": "", | |
"Google Shopping / Custom Label 4": "", | |
"Variant Image": "", | |
"Variant Weight Unit": "g", | |
"Variant Tax Code": "", | |
"Cost per item": "", | |
"Price / International": "", | |
"Compare At Price / International": "", | |
"Status": "active" | |
} | |
if len(images)>1: | |
for img in images: | |
empty_set = { | |
"Handle": slugify(dt["Name"]), | |
"Title": "TEST: "+dt["Name"], | |
"Body (HTML)": dt["Short description"], | |
"Vendor": "", | |
"Product Category": dt["Categories"], | |
"Type": dt["Type"], | |
"Tags": dt["Tags"], | |
"Published": "TRUE", | |
"Option1 Name": "Title", | |
"Option1 Value": dt["Name"], | |
"Option2 Name": "", | |
"Option2 Value": "", | |
"Option3 Name": "", | |
"Option3 Value": "", | |
"Variant SKU": "", | |
"Variant Grams": "", | |
"Variant Inventory Tracker": "", | |
"Variant Inventory Qty": "", | |
"Variant Inventory Policy": "deny", | |
"Variant Fulfillment Service": "manual", | |
"Variant Price": dt["Regular price"], | |
"Variant Compare At Price": "", | |
"Variant Requires Shipping": "TRUE", | |
"Variant Taxable": "TRUE", | |
"Variant Barcode": "", | |
"Image Src": img, | |
"Image Position": "1", | |
"Image Alt Text": "", | |
"Gift Card": "FALSE", | |
"SEO Title": "Our awesome T-shirt in 70 characters or less.", | |
"SEO Description": "A great description of your products in 320 characters or less", | |
"Google Shopping / Google Product Category": "Apparel & Accessories > Clothing", | |
"Google Shopping / Gender": "Unisex", | |
"Google Shopping / Age Group": "Adult", | |
"Google Shopping / MPN": "7X8ABC910", | |
"Google Shopping / AdWords Grouping": "T-shirts", | |
"Google Shopping / AdWords Labels": "cotton, pre-shrunk", | |
"Google Shopping / Condition": "used", | |
"Google Shopping / Custom Product": "FALSE", | |
"Google Shopping / Custom Label 0": "", | |
"Google Shopping / Custom Label 1": "", | |
"Google Shopping / Custom Label 2": "", | |
"Google Shopping / Custom Label 3": "", | |
"Google Shopping / Custom Label 4": "", | |
"Variant Image": "", | |
"Variant Weight Unit": "g", | |
"Variant Tax Code": "", | |
"Cost per item": "", | |
"Price / International": "", | |
"Compare At Price / International": "", | |
"Status": "active" | |
} | |
total_data.append(empty_set) | |
else: | |
total_data.append(sformat) | |
# Ensure that total_data is a list of dictionaries | |
if isinstance(total_data, tuple): | |
total_data = list(total_data) | |
pprint(total_data) | |
# with open('final_out.json') as json_file: | |
# ndata = json.load(json_file) | |
json_to_csv(total_data, "final-out.csv") | |
# print(len(data)) | |
# pprint(data[0]) | |
if __name__ == "__main__": | |
process() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment