Reputation: 191
I have the following nested dictionary my_dict
(which was originally JSON from a response to a request to an endpoint):
my_dict = {'SalesStatusResult': {'next_offset': -1,
'customers': [{'caCustomerID': 'N872-1665582527-adhoc-0:0',
'identifier': 'N872',
'customertype': '',
'blocked': False,
'location': 'London',
'expected_transaction_time': {'epoch': 1665582527,
'tz': 'EDT',
'dow': 'Wednesday',
'time': '09:48',
'date': '10/12/2022',
'localtime': 1665568127},
'estimated_order_time': {'epoch': 1665582527,
'tz': 'EDT',
'dow': 'Wednesday',
'time': '09:48',
'date': '10/12/2022',
'localtime': 1665568127},
'actual_order_time': {'epoch': 1665582527,
'tz': 'EDT',
'dow': 'Wednesday',
'time': '09:48',
'date': '10/12/2022',
'localtime': 1665568127},
'expected_settlement_time': {'epoch': 0,
'tz': '',
'dow': '',
'time': '',
'date': '',
'localtime': 0},
'status': 'Completed',
'status_code': 1,
'progress_percent': 100,
'state_code': 2,
'cust_id' : 'AD-17-C18'
}]}}
Notably, when calling type
on the object, it showed dict
, not JSON.
I would like to extract the value in the caCustomerID
key (which is "N872-1665582527-adhoc-0:0
").
Separately, I would also like to get the cust_id
, which is "AD-17-C18
".
How do I do this?
Thanks!
Upvotes: -1
Views: 46
Reputation: 40773
Here is a hint: starting from where you want and go upward: caCustomerID -> customers (this is a list) -> SalesStatusResult (this is a dictionary). After that, writing code which traverse back down:
for record in my_dict["SalesStatusResult"]["customers"]:
print(record["caCustomerID"])
print(record["cust_id"])
Output:
N872-1665582527-adhoc-0:0
AD-17-C18
Upvotes: 1