This will get you your expected result for your example input:
data["orders"]["order"][0]["lineitems"]["lineitem"][0]["product"]
Although, where I have [0]
it is getting the first item in those lists. Your example has only one item in the lists but if there are more, you will need to iterate over them to get the result(s) you want.
This is based on your example data:
data = {
'numreturned': 1,
'orders': {'order': [{'admin_requestor_id': 0,
'amount': '12.00',
'contactid': 0,
'currencyprefix': '$',
'currencysuffix': ' USD',
'date': '2022-08-05 16:39:18',
'frauddata': '',
'fraudmodule': '',
'fraudoutput': '',
'id': 3267,
'invoiceid': 101,
'ipaddress': '111.111.111.111',
'lineitems': {'lineitem': [{'amount': '$12.00 USD',
'billingcycle': 'Monthly',
'domain': 'chz',
'product': 'Dedicated '
'Server Windows',
'producttype': 'Dedicated/VPS '
'Server',
'relid': 3648,
'status': 'Active',
'type': 'product'}]},
'name': 'Max Vorenberg',
'nameservers': '',
'notes': '',
'orderdata': '[]',
'ordernum': 13424555,
'paymentmethod': 'usd',
'paymentmethodname': 'Cryptocurrencies',
'paymentstatus': 'Paid',
'promocode': '',
'promotype': '',
'promovalue': '',
'renewals': '',
'requestor_id': 2173,
'status': 'Active',
'transfersecret': '',
'userid': 2132,
'validationdata': ''}]},
'result': 'success',
'startnumber': 0,
'totalresults': 1
}
Explanation
To break this down I am first getting data["orders]
which contains this dictionary:
{
'order': [{'admin_requestor_id': 0,
...
'lineitems': {'lineitem': [{'amount': '$12.00 USD',
'billingcycle': 'Monthly',
'domain': 'chz',
'product': 'Dedicated Server Windows',
'producttype': 'Dedicated/VPS Server',
'relid': 3648,
'status': 'Active',
'type': 'product'}]},
...
]
}
This dictionary only has one key "orders", which contains a list containing a single dictionary. If you expect it to have more than one dictionary in it then you will need to loop through it like so:
for order in data["orders"]["order"]:
...
In the ...
you would have a single order like the following:
{
'admin_requestor_id': 0,
...
'lineitems': {'lineitem': [{'amount': '$12.00 USD',
'billingcycle': 'Monthly',
'domain': 'chz',
'product': 'Dedicated Server Windows',
'producttype': 'Dedicated/VPS Server',
'relid': 3648,
'status': 'Active',
'type': 'product'}]},
...
}
This has a key "lineitems" which is a dictionary containing a single key "lineitem" which is another list of dictionaries and like above you can iterate this as well. Our code thus far would become:
for order in data["orders"]["order"]:
for item in order["lineitems"]["lineitem"]:
...
Now in the inner loop we have a dictionary like the following:
{
'amount': '$12.00 USD',
'billingcycle': 'Monthly',
'domain': 'chz',
'product': 'Dedicated Server Windows',
'producttype': 'Dedicated/VPS Server',
'relid': 3648,
'status': 'Active',
'type': 'product'
}
This dictionary has the key "product" which is the value we are looking for. Our iterative approach now becomes:
for order in data["orders"]["order"]:
for item in order["lineitems"]["lineitem"]:
print(item["product"])