Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Get Requests JSON parse dictionary issue...

Sooo I know the title is insane, let me explain. Im download a post request, parsing it into json then attempting to get certain data from it.


This is my current code
Code
r = requests.get("https://shoppy.gg/api/v1/orders/", headers=headers)
jsonObject = json.loads(str(r.text))
for key in jsonObject:
                                       
   print(key)


Ofc there is more before it but it doesent matter...

so my issue is when I print the key I want to access a certain part of the data. This works with one peice, but not another..

Here is an example of what the key looks like.
Code
{'id': 'd781fbfe-f746-4b7-a990-9eaa2887894d', 'pay_id': None, 'product_id': 'pfFjYt', 'coupon_id': None, 'price': 10, 'currency': 'USD', 'exchange_rate': None, 'email': '[email protected]', 'delivered': 0, 'confirmations': 0, 'required_confirmations': 1, 'transaction_id': '', 'crypto_address': '[email protected]', 'crypto_amount': '10', 'shipping_address': None, 'quantity': 1, 'gateway': 'PayPal', 'custom_fields': [{'name': 'Discord Name (FULL NAME WITH #)', 'type': 'text', 'required': True, 'value': 'I NEED THIS PORTION OF DATA'}], 'refund_id': None, 'is_replacement': False, 'replacement_id': None, 'paid_at': None, 'disputed_at': None, 'created_at': '2020-04-23T09:47:41.000000Z', 'is_partial': None, 'crypto_received': None, 'return_url': None, 'coupon': [], 'product': {'id': 'xxxx', 'attachment_id': None, 'title': 'XXXX', 'description': None, 'image': None, 'unlisted': False, 'type': 'service', 'price': 10, 'currency': 'USD', 'email': {'enabled': False}, 'stock_warning': 0, 'quantity': {'min': 1, 'max': 1}, 'confirmations': 3, 'custom_fields': [{'name': 'Discord Name (FULL NAME WITH #)', 'type': 'text', 'required': True}], 'gateways': ['PayPal'], 'webhook_urls': [None], 'dynamic_url': '', 'position': None, 'created_at': '2020-04-23T09:36:58.000000Z', 'updated_at': '2020-04-23T09:36:58.000000Z', 'stock': 9223372036854775807, 'accounts': []}, 'agent': {'geo': {'ip': 'XXXX', 'iso_code': 'US', 'country': 'United States', 'city': 'XXXXX', 'state': 'XX', 'state_name': 'XXXX', 'postal_code': 'XXXX', 'lat': XXXX, 'lon': XXXX, 'timezone': 'America/New_York', 'continent': 'NA', 'currency': 'USD', 'default': True, 'cached': False}, 'data': {'is_mobile': False, 'is_tablet': False, 'is_desktop': True, 'platform': 'Windows', 'browser': {'name': 'Chrome', 'version': '81.0.4044.122'}, 'device': {'name': 'WebKit', 'version': False}}}, 'hash': 'd2f1b70fac94c3790ca9253934924e794d6fc4aa6277ea0d401b5d7b0f759cce'}


So. Im trying to print this portion of data out.
'value': 'I NEED THIS PORTION OF DATA'
if I do
Code
print(key['id'])
it works and returns the ID 'd781fbfe-f746-4b7-a990-9eaa2887894d'


BUTTTTT IF i do
Code
print(key['value'])
It will return with KeyError: 'value'


Solved my issue!


Code
r = requests.get("https://shoppy.gg/api/v1/orders/", headers=headers)
jsonObject = json.loads(str(r.text))
for key in jsonObject:
                                       
   t = str(key["custom_fields"])

   if t != "None":
       t = t.replace("[", "")
       t = t.replace("]", "")
       t = t.replace("'", '"')
       res = ast.literal_eval(t)  
   
       print(res["value"])
[Image: F2G97kn.png]
Code
data = {"custom_fields":[{'name': 'Discord Name (FULL NAME WITH #)', 'type': 'text', 'required': True, 'value': 'I NEED THIS PORTION OF DATA'}]}
print data['custom_fields'][0]['value']


did u try like this ? ur code ugly.
In my IDE I get this when I copy past that dictionary.

Code
NameError: name 'XXXX' is not defined

That's not a properly structured python dictionary, it's super disorganized. It has all sorts of issues with it.
[Image: Iu0EfTL.gif]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by evilgnomeJan 11, 2022 05:39 AM 2 195 Jan 11, 2022 05:39 AM
Last Post: Charger
[Python] by roosiinfidel09May 26, 2021 06:32 AM 1 274 May 26, 2021 06:32 AM
Last Post: h0lyl337
[.NET] by outlawNov 28, 2020 04:25 AM 12 367 Nov 28, 2020 04:25 AM
Last Post: Rmdy