1

Looking for a generic solution where I can remove the specific key and its value from dict. For example, if dict contains the following nested key-value pair:

data={

  "set": {
  "type": "object", #<-- should remove this key:value pair
  "properties": {
    "action": {
      "type": "string",  #<-- should NOT remove this key:value pair
      "description": "My settings"
    },
    "settings": {
      "type": "object", #<-- should remove this key:value pair
      "description": "for settings",
      "properties": {
        "temperature": {
          "type": "object", #<-- should remove this key:value pair
          "description": "temperature in degree C",
          "properties": {
            "heater": {
              "type": "object", #<-- should remove this key:value pair
              "properties": {
                "setpoint": {
                  "type": "number"
                },
              },
              "additionalProperties": false
            },

          },
          "additionalProperties": false
        },

      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false
}
}

I want an output dict without "type":"object" across the occurrence of this key:value pair. The expected output should produce the result without "type":"object"

2

2 Answers 2

7

You can write a recursive function:

def remove_a_key(d, remove_key):
    if isinstance(d, dict):
        for key in list(d.keys()):
            if key == remove_key:
                del d[key]
            else:
                remove_a_key(d[key], remove_key)

and call it as:

remove_a_key(data, 'type')

This recursively removes 'type' key and it's value from each nested dictionary no matter how deep it is.

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

3 Comments

Your logic remove all occurrences of key 'type'. This means the logic removes type:'object' which is expected but it also removes type':'string' which is not an expected result. Hope I made it clear on the requirement.
Thanks for the logic. Modified to meet my requirement. Please review the modified logic. def remove_a_key(d, remove_key, remove_val): if isinstance(d, dict): for key in list(d.keys()): if key == remove_key and d[key] == remove_val: del d[key] else: remove_a_key(d[key], remove_key,remove_val) import json with open('test.json') as f: myjson = json.load(f) path = [] remove_a_key(myjson, 'type','string')
@shekarchandra, Your code looks fine. Maybe I read wrong, I answered for the removal of all keys without taking values into consideration. But hope my answer was helpful. Kindly, accept a helpful answer from the two already posted by clicking a tick on the left of the answer (This helps the community identify the helpful solution). Thanks.
1

Use python module nested-lookup to play with any kind of nested documents. Checkout https://pypi.org/project/nested-lookup/ for more info.

In your case you need to use method nested_delete to delete all occurrences of a key.

Usage:

from nested_lookup import nested_delete

print(nested_delete(data, 'type'))

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.