0

I just wanted to ask what can I do to solve this issue I have.

Essentially I am making a stock checker for sneakers from Adidas, I know the endpoint to obtain the stock but the JSON data given back to me whilst readable and contains what I need also contains a bunch of other information that is unnecessary to what I am trying to do.

Example of a link to an endpoint:

http://production.store.adidasgroup.demandware.net/s/adidas-GB/dw/shop/v16_9/products/(BZ0221)?client_id=c1f3632f-6d3a-43f4-9987-9de920731dcb&expand=availability,variations,prices

This is a link to the JSON containing the stock of the shoe, price and availability. However, if you try to open it you'll see that it responds a bunch of useless info such as the description of the shoe and the price which I do not need.

A github repository that I was using to try and get to grips with the requests I am trying to make is:

https://github.com/yzyio/adidas-stock-checker/blob/master/assets/index.js

I can get it to give me the JSON response I am just trying to strip what I don't need and keep what I do need which I am finding very difficult especially in python.

Many Thanks!

Fader910
  • 71
  • 1
  • 4
  • Have you taken a look at this? https://stackoverflow.com/questions/4917006/string-to-dictionary-in-python – Sasha Aug 18 '17 at 14:22
  • just choose what fields of the json file are useful for you... – coder Aug 18 '17 at 14:22
  • Possible duplicate of [Parsing values from a JSON file?](https://stackoverflow.com/questions/2835559/parsing-values-from-a-json-file) – stovfl Aug 18 '17 at 15:54

1 Answers1

2

Since you've said you can get a JSON response from the server than the first think you need to do is tell python to load it as JSON.

import json
data = json.loads(response_from_server)

After doing this you can now access the values in your JSON object the way you would access them via a Python dict.

data["artist"]["id"]
Chris Hawkes
  • 11,923
  • 6
  • 58
  • 68
  • Hi, I tried doing thsi with the objects "name" and "variants" as a test but I end up getting this `TypeError: the JSON object must be str, bytes or bytearray, not 'Response'` – Fader910 Aug 18 '17 at 14:33
  • That means response_from_server is not a string, try evaluating what type of object this is.... if it's a response object than you may need to say data = json.loads(response_from_server.text) – Chris Hawkes Aug 18 '17 at 14:37
  • 1
    see this question for more information. https://stackoverflow.com/questions/42976090/json-object-must-be-str-not-response – Chris Hawkes Aug 18 '17 at 14:38
  • 1
    It's best to use `.content`, `.text` will try to decode the response. Or even better use `.json()` which returns a json object. – t.m.adam Aug 18 '17 at 14:42