Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access a dictionary value with dynamic path using in python

I have this piece of code to extract a value from a dictionary object

extracted_value = response_content["retrievePolicyBillingSummariesResponse"]["billingSummaries"]["policyBillingSummary"][0]["billingSummary"]["lastPayment"]["status"]
extracted_value = response_content["retrievePolicyBillingSummariesResponse"]["billingSummaries"]["policyBillingSummary"][0]["billingSummary"]["bill"]["dueDate"]

These are just two samples but I have a dozen of these with different key/path combinations. How can I just call these using a module and do something like this

def get_value_from_content (response_content, my_path):
    # how can I use the value in my_path as the key instead of this hard coded path ?
    extracted_value =  response_content["retrievePolicyBillingSummariesResponse"]["billingSummaries"]["policyBillingSummary"][0]["billingSummary"]["lastPayment"]["status"]
    #extracted_value = response_content using my_path is what I would like to do
    return extracted_value

#I get this from a REST API call but skipping the code here and just hard coding to ask the question here
response_content = {u'retrievePolicyBillingSummariesResponse': {u'billingSummaries': {u'policyBillingSummary': [{u'policy': {u'status': u'A', u'policyNumber': u'xyz123', u'writingCompany': u'FBI', u'renewalFlag': u'false', u'convertedRenewalOffer': u'false', u'termExpirationDate': u'2017-06-26', u'lineOfBusiness': u'PC', u'termEffectiveDate': u'2016-06-26', u'riskState': u'CA', u'insureds': {u'namedInsuredSummary': [{u'preferredPostalAddress': {u'streetAddressLine': u'1 disney', u'cityName': u'palo alto', u'zipCode': u'94100', u'isoRegionCode': u'CA'}, u'name': {u'lastName': u'DOE', u'fullName': u'john doe', u'firstName': u'john'}}]}, u'additionalInterests': {u'additionalInterest': [{u'billTo': u'N', u'name': {u'partyType': u'Organization'}}]}, u'type': u'PA', u'statusDescription': u'Active', u'dataSource': u'from_heaven'}, u'billingSummary': {u'paymentRestriction': u'false', u'nextInstallmentAmount': u'0.00', u'bill': {u'installmentNumber': u'1', u'statementDate': u'2016-06-26', u'paymentPlan': u'Direct', u'installmentAmount': u'12.00', u'totalBillAmountDue': u'1.76', u'previousBalance': u'0.00', u'dueDate': u'2016-06-26', u'billingPlan': u'ANN'}, u'lastPayment': {u'status': u'A'}, u'currentBalance': u'16.66', u'payOffAmount': u'15.66', u'isRestrictedToPay': u'false'}}]}}}

my_path = '["retrievePolicyBillingSummariesResponse"]["billingSummaries"]["policyBillingSummary"][0]["billingSummary"]["lastPayment"]["status"]'
get_extracted_item = get_value_from_content(response_content,my_path)

my_path = '["retrievePolicyBillingSummariesResponse"]["billingSummaries"]["policyBillingSummary"][0]["billingSummary"]["bill"]["dueDate"]'
get_extracted_item = get_value_from_content(response_content,my_path)
like image 923
Naresh MG Avatar asked Oct 21 '25 12:10

Naresh MG


1 Answers

first of all it will be easier to write small utility function like

def extract_from_dictionary(dictionary, *keys_or_indexes):
    value = dictionary
    for key_or_index in keys_or_indexes:
        value = value[key_or_index]
    return value

as we can see from your example there is an object called billingSummary that appears in required paths, so we can avoid boilerplate with

def get_billing_summary(response_content):
    return extract_from_dictionary(
        response_content,
        "retrievePolicyBillingSummariesResponse",
        "billingSummaries",
        "policyBillingSummary",
        0,
        "billingSummary")

then we can simply write

def get_value_from_content(response_content, *keys):
    billing_summary = get_billing_summary(response_content)
    extracted_value = extract_from_dictionary(billing_summary,
                                              *keys)
    return extracted_value

and obtain required objects like

last_payment_status = get_value_from_content(response_content,
                                             "lastPayment",
                                             "status")
bill_due_date = get_value_from_content(response_content,
                                       "bill",
                                       "dueDate")
print("last_payment_status:", last_payment_status)
print("bill_due_date:", bill_due_date)

gives us

last_payment_status: A
bill_due_date: 2016-06-26
like image 190
Azat Ibrakov Avatar answered Oct 23 '25 09:10

Azat Ibrakov