0

I need to remove a nested key-value from a python json object. The path to this nested object in the json is given to me in a string.

I can do this with del command if I hard-code the path the nested object. However, I can't figure out how to de-reference the string to get the nested object.

Thus in the following code snippet, the object is unchanged after the first del, but the key-value is removed after the second del.

from pprint import pprint
input_obj = [
        {
            "version": "2021a",
            "resource": {
                "resourceType": "human",
                "id": "9451bf03-665c-4b4f-9836-066b4185334c",
                "attributes": [
                    {
                        "attribute": "name",
                        "value": 
                             {"firstname": "John", 
                              "last name": "Doe"}
                    },
                    {
                        "attribute": "weight",
                        "value": "170"
                    }                 
                ]
            }
        }
    ]

# fails
mypath = "input_obj" + "[0]['resource']['attributes'][0]['value']"
del mypath    
pprint (input_obj)

# works
del input_obj[0]['resource']['attributes'][0]['value'] 
pprint (input_obj)

Output from first pprint:

[{'resource': {'attributes': [{'attribute': 'name',
                               'value': {'firstname': 'John',
                                         'lastname': 'Doe'}},
                              {'attribute': 'weight', 'value': '170'}],
               'id': '9451bf03-665c-4b4f-9836-066b4185334c',
               'resourceType': 'human'},
  'version': '2021a'}]

Output from second pprint. The nested key 'value' and value structure are removed.

[{'resource': {'attributes': [{'attribute': 'name'},
                              {'attribute': 'weight', 'value': '170'}],
               'id': '9451bf03-665c-4b4f-9836-066b4185334c',
               'resourceType': 'human'},
  'version': '2021a'}]

The first del is deleting the variable mypath, not the referenced object. The second del works because it refers to an actual part of the object.

How can I de-reference the string or somehow point to the object the same way as the hard reference?

1 Answer 1

1

Using the exec() command works.

mypath = "input_obj" + "[0]['resource']['attributes'][0]['value']"
exec('del ' + mypath)
pprint (input_obj) 

Output:

[{'resource': {'attributes': [{'attribute': 'name'},
                              {'attribute': 'weight', 'value': '170'}],
               'id': '9451bf03-665c-4b4f-9836-066b4185334c',
               'resourceType': 'human'},
  'version': '2021a'}]

Kudos to James Welch for his suggestion to use eval(), even though it didn't work. Searching on why eval() was failing I came across another post which recommended exec() rather than eval().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.