0

i am making something to monitor a site for new products, so im trying to add all the titles to a json file, that starts out with {"product_titles": []}, im trying to figure our how to add the dicts that contain the product title and sizes to the empty list this is my code

import requests
import json

url = 'https://www.supremenewyork.com/mobile_stock.json'
headers = {
    'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1',
    'Accept': 'application/json',
    'Accept-Encoding': 'br, gzip, deflate',
    'Accept-Language': 'en-us'
}


req = requests.get(url, headers=headers)#

page = req.json()
categories = page['products_and_categories']
Sweatshirts = categories['Sweatshirts']


product_list = []
for sweater in Sweatshirts:
    product_name = sweater['name']
    product_colors = []
    product_sizes = []
    product_stock_levels = []
    #print(product_name)
    raw_product_info = requests.get('https://www.supremenewyork.com/shop/' + str(sweater['id']) + '.json', headers=headers)
    product_info = raw_product_info.json()
    styles = product_info['styles']
    for style in styles:
        colors = style['name']
        full_product_name = product_name + colors
        file = open
        product_colors.append(colors)
        for size in style['sizes']:
            sizes = {size['name'] : size['stock_level']}
            product_sizes.append(sizes)
            with open('supreme.json', 'r+') as supremef:
                data = json.load(supremef)
                dump = json.dump(data['product_titles'].append({full_product_name: sizes}), supremef)


the last few lines in where i try to add it to the list in the json file but its not adding it to it

Kendall Kelly
  • 121
  • 1
  • 10
  • Please check if you showed us the whole code, at least the import of the `requests` module seems to be missing. – Maurice Mar 10 '19 at 15:41
  • 1
    i just added it in, it was editted out for some reason – Kendall Kelly Mar 10 '19 at 15:46
  • 1
    Can you verify that `data` get's updated correctly before you dump it with `data['product_titles'].append({full_product_name: sizes})` ? i.e. check, that the list of `'product_titles'` gets extended as expected? Without seeing the supreme.json this is difficult to debug ;-) – Maurice Mar 10 '19 at 15:52
  • 1
    `.append()` adds items but returns None. – t.m.adam Mar 10 '19 at 15:54
  • yea thats the problem im getting, im trying to figure out how to add it to list in the json file – Kendall Kelly Mar 10 '19 at 15:59

1 Answers1

1

As pointed out by @t.m.adam in the comments - here's a little explanation.

You expect the append-operation to return the whole object, which json.dump is then supposed to save.

This example shows this doesn't work, as append returns None.

>>> mydict = {"items": [1,2,3,4]}
>>> type(mydict["items"].append(5))
<class 'NoneType'>
>>> print(mydict)
{'items': [1, 2, 3, 4, 5]}
>>> 

See also this question for a discussion on this behaviour, I couldn't find an entry in the Python docs for this.

Your code should probably look like this:

data = json.load(supremef)
data['product_titles'].append({full_product_name: sizes})
dump = json.dump(data, supremef)

Slightly off topic:

Your file = open probably doesn't do what you expect.

Maurice
  • 11,482
  • 2
  • 25
  • 45