Trying to delete a key/value pair from JSON within a list

  dictionary, json, list, python

enter image description here

def loop_through_list_keys_and_update_or_delete(body_dict, list_key, key_to_manipulate, update_or_delete):
request_body = body_dict
try:

    for keys in request_body[list_key]:
        for key, value in keys.items():
            if key == key_to_manipulate:
                if update_or_delete == 'delete':
                    del request_body[key]

Hi all! Relatively new to Python, so please be gentle and I definitely appreciate your help! In the attached picture are my run time values. Basically what I am trying to do is delete a specific (but one that changes) key/value pair. The request_body is my json that I’ve read in.

I am looping on the list ‘fxTransactions’ and the only keys it is getting are financialTransaction and transactionRefId. In this case, I need to get to the rule field that is within financialTransaction and delete it and its value. I can’t seem to figure out how to get to this field and delete it. Note that the field/value pair to delete is dynamic so I will not always know which one I want to delete (could be debtor, creditor, etc). I’d also like to use this code for other lists, if possible, so trying not to hard-code. But I can if there is no other way.

Thank you in advance!

Source: Python Questions

LEAVE A COMMENT