Consider a "complex" schema which uses references from other files like the one below. Is there a simple way to get a list of all properties defined in a schema, perhaps using some of the machinery already in python-jsonschema
? I think I only need properties at the top level, but a generalizable approach would be great.
There are numerous applications I need this for, but basically I need to be able to run different operations on only those 'properties' of objects matching the schema which are defined in the schema (so that I don't also apply these operations to any additional properties not defined in the schema). For example, lets say I want to print the value of every property than an object has, that is defined in the schema. To do that I need a list of the properties the schema defines. Another example would be if I want to compare two objects based only on the schema-defined properties.
{
"$schema": "http://json-schema.org/schema#",
"allOf": [
{
"$ref": "file:/some/file.json"
}
],
"properties": {
"a": {
"type": "string"
}
}
}
Example application:
# Load an object that has attributes conforming to the schema in `schema`
obj = Schema_Conforming_Object(schema)
# Get a list of all properties defined in the schema
# This is what I'm not sure how to do nicely
props = properties_of_schema(schema)
for pr in props:
if pr in obj:
print(obj[pr])