[Python] Validate JSON Schema which has fixed keys and user defined keys in Python

Discussão em 'Python' iniciado por Stack, Maio 15, 2025.

  1. Stack

    Stack Membro Participativo

    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
    }

    Continue reading...

Compartilhe esta Página