Reputation: 727
I am looking for Python module to filter JSON data against schema.
For example, there is JSON data:
{
"system" : {
"state" : "enabled",
"id" : 5,
"keys" : [
{ "key_id": 12, "key": "filename.key" }
]
}
}
And there is JSON schema:
{
"system": {
"id": "system",
"required": true,
"type": "object",
"properties": {
"state": {
"id": "state",
"required": true,
"type": "string"
},
"id": {
"id": "id",
"required": true,
"type": "number"
}
}
}
}
As you can see, the schema does not contain "keys" property.
I need some tool, which could filter the JSON data using the schema and provide following JSON as an output:
{
"system" : {
"state" : "enabled",
"id" : 5
}
}
Upvotes: 5
Views: 1884
Reputation: 727
Since there is no tool, for filtering JSON data against schema, I have resolved my task as follows.
Created template of expected JSON file. Actually it is already filtered JSON file, but without data.
{
"system" : {
"state" : "",
"id" : 0
}
}
Then go through the data file and the template file and just copy values from one to another for properties that exist in both files.
Upvotes: 2
Reputation: 443
Purpose of JSON schema is to validate given JSON input against a defined schema. As @Relequestual says in a comment you cannot use JSON schema to filter out fields directly.
If you need to remove only keys
field then you do not need to use JSON schema at all. You could simply remove the field from JSON input.
In case you need to filter out a bunch of unexpected fields from the input you could use JSON schema to identify
those fields. But you need to do filtering part manually or using another library since JSON schema cannot do that for you.
You could use additionalProperties
field to restrict unexpected keys.
{
"type":"object",
"required":false,
"properties":{
"system": {
"id": "system",
"required": true,
"type": "object",
"properties": {
"state": {
"id": "state",
"required": true,
"type": "string"
},
"id": {
"id": "id",
"required": true,
"type": "number"
}
},
"additionalProperties": false
}
}
}
This will give a validation error like following
Message: Property 'keys' has not been defined and the schema does not allow additional properties. Schema path: #/properties/system/additionalProperties
This may not be the exact answer you are looking for. But hope it helps.
Upvotes: 1
Reputation: 129
You can use jsonschema
to validate your json against the schema, check this example
from jsonschema import validate
schema = {"type" : "object","properties" : { "price" : {"type" : "number"},"name" : {"type" : "string"},},}
validate(instance={"name" : "Eggs", "price" : 34}, schema=schema)
If no exception is raised by validate(), the instance is valid
Upvotes: 1