I’m trying to validate a JSON file that is provided by a user. The JSON will contain certain fixed keys, but also contain some user-defined keys too. I want to validate that this JSON object contains these fixed keys, in a certain format, and the user-defined keys are in a certain format too (as these keys will always have values in a defined format).
I came across this post Validate JSON data using python, but the documentation for jsonschema.validate
doesn’t really show anything to do with user-defined keys, and also how to define if a key should have a list of dicts, or a dict which its key-values must be of a list of dicts.
Here’s a sample schema:
{
"a": "some value",
"b": "some value",
"c": {
"custom_a": [{...}],
"custom_b": [{...}]
},
"d": [{...}]
}
I have tried doing the following:
import json
from jsonschema import validate
my_json = json.loads(<JSON String following above pattern>)
schema = {
"a" : {"type": "string"},
"b" : {"type": "string"},
"c" : {[{}]},
"d": [{}]
}
validate(instance=my_json, schema=schema) #raises TypeError on "c" and "d" in schema spec
I have also tried the following schema spec, but I get stuck on how to handle the custom keys, and also nested lists within dicts, etc.
schema = {
"a" : {"type": "string"},
"b" : {"type": "string"},
"c" : {
"Unsure what to define here": {"type": "list"} #but this is a list of dicts
},
"d": {"type": "list"} #but this is a list of dicts
}
2
Answers
Define the known properties as usual and the unknown properties as
additionalProperties
with a schema definedThis will allow an instance such as
There are several Python libraries available for validating JSON data, especially when it comes to complex schemas with fixed and user-defined keys. Here are some commonly used libraries, each with unique strengths and options for managing dynamic structures.
The most common are-
Using jsonschema,
Using marshmallow,
Using pydantic,
Output when I ran all of them at once