I am using Python's jsonschema to validate JSON files against a schema. It works great. But now I need to remove any properties from my JSON that are not present in the schema.
I know that according to the JSON Schema docs, I can set the property:
additionalProperties = false
to reject any files with additional properties. But this will just reject the properties, not actually remove them.
What is the best way to remove them?
I guess I can write my own script that:
- walks every leaf node of the JSON file
- checks whether the leaf node exists in the schema
- if it does not, walks up the tree until it finds the highest node that does exist, then prunes the branch at that point.
My question is: is there an existing Python library to do this, or do I need to write one? I have Googled, but without any success.