Home > Enterprise >  Removing nested json key-value pair from object by reference in python
Removing nested json key-value pair from object by reference in python

Time:11-14

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?

CodePudding user response:

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, even though it didn't work. Searching on why eval() was failing I came across another post which recommended exec() rather than eval().

CodePudding user response:

Are you just trying to remove the value or remove the key-value pair from the hierarchy? It is difficult to tell without the outputs after the del statements

  • Related