-1

This is the code i have so far:

import json
import requests
import time

endpoint = "https://www.deadstock.ca/collections/new-arrivals/products/nike- 
air-max-1-cool-grey.json"
req = requests.get(endpoint)
reqJson = json.loads(req.text)

for id in reqJson['product']:
    name = (id['title'])
    print (name)

Feel free to visit the link, I'm trying to grab all the "id" value and print them out. They will be used later to send to my discord.

I tried with my above code but i have no idea how to actually get those values. I don't know which variable to use in the for in reqjson statement

If anyone could help me out and guide me to get all of the ids to print that would be awesome.

for product in reqJson['product']['title']:
    ProductTitle = product['title']
    print (title)
TNC
  • 11
  • 5
  • Just access the `dict` values.... take a look : https://stackoverflow.com/questions/5404665/accessing-elements-of-python-dictionary – rafaelc Jul 17 '18 at 02:55
  • what would i put in the `for _ in reqjson[__]` though? – TNC Jul 17 '18 at 03:01

1 Answers1

1

I see from the link you provided that the only ids that are in a list are actually part of the variants list under product. All the other ids are not part of a list and have therefore no need to iterate over. Here's an excerpt of the data for clarity:

{
    "product":{
        "id":232418213909,
        "title":"Nike Air Max 1 \/ Cool Grey",
        ...
        "variants":[
            {
                "id":3136193822741,
                "product_id":232418213909,
                "title":"8",
                ...
            },
            {
                "id":3136193855509,
                "product_id":232418213909,
                "title":"8.5",
                ...
            },
            {
                "id":3136193789973,
                "product_id":232418213909,
                "title":"9",
                ...
            },
            ...
       ],
        "image":{
            "id":3773678190677,
            "product_id":232418213909,
            "position":1,
            ...
        }
    }
}

So what you need to do should be to iterate over the list of variants under product instead:

import json
import requests

endpoint = "https://www.deadstock.ca/collections/new-arrivals/products/nike-air-max-1-cool-grey.json"
req = requests.get(endpoint)
reqJson = json.loads(req.text)

for product in reqJson['product']['variants']:
    print(product['id'], product['title'])

This outputs:

3136193822741 8
3136193855509 8.5
3136193789973 9
3136193757205 9.5
3136193724437 10
3136193691669 10.5
3136193658901 11
3136193626133 12
3136193593365 13

And if you simply want the product id and product name, they would be reqJson['product']['id'] and reqJson['product']['title'], respectively.

blhsing
  • 91,368
  • 6
  • 71
  • 106
  • wow no way. I was so close to this when i was changing the code up. Could you explain to me why you put `for product in reqJson['product']['variants']:` please? I have no clue why the product and variants are used. Just want a better understanding so i dont have to ask again lol – TNC Jul 17 '18 at 03:04
  • No problem. I've updated my answer with better explanations. – blhsing Jul 17 '18 at 03:10
  • That makes alot more sense, now if i were to only access the product name, how would i do so because it isnt under the variants? Do i just use product? – TNC Jul 17 '18 at 03:14
  • The product name would be `reqJson['product']['title']`. I've updated my answer to include this note. – blhsing Jul 17 '18 at 03:22
  • I edited my post to show what i have done, im getting "TypeError: string indices must be integers" so, not sure if i have done what you have explained. – TNC Jul 17 '18 at 03:24
  • You are trying iterate over `product`, which is only a dict, not a list. If you want the product names of the variants, you should iterate through `reqJson['product']['variants']`. I've updated my answer to show you the product (variant) names as well. – blhsing Jul 17 '18 at 03:41
  • OHHH i was meaning the actual product title, `"title":"Nike Air Max 1 \/ Cool Grey"` its right at the beginning of the json and doesnt fall under the variants. – TNC Jul 17 '18 at 03:43
  • Yup. No need to use a loop if all you want is the product title. By the way can you mark this answer as accepted if you think it is correct? – blhsing Jul 17 '18 at 03:53