Apr 23, 2020 03:40 AM
(This post was last modified: Apr 23, 2020 03:52 AM by Xogg.)
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
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.
So. Im trying to print this portion of data out.
'value': 'I NEED THIS PORTION OF DATA'
if I do
it works and returns the ID 'd781fbfe-f746-4b7-a990-9eaa2887894d'
BUTTTTT IF i do
It will return with KeyError: 'value'
Solved my issue!
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'])BUTTTTT IF i do
Code
print(key['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]](https://i.imgur.com/F2G97kn.png)

![[Image: Iu0EfTL.gif]](https://i.imgur.com/Iu0EfTL.gif)