“Python Jsonschema” Kode Jawaban

jsonschema di Python

import json
import jsonschema
from jsonschema import validate

# Describe what kind of json you expect.
studentSchema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "rollnumber": {"type": "number"},
        "marks": {"type": "number"},
    },
}

def validateJson(jsonData):
    try:
        validate(instance=jsonData, schema=studentSchema)
    except jsonschema.exceptions.ValidationError as err:
        return False
    return True
Rich Raccoon

Python Jsonschema

from jsonschema import validate

>>> # A sample schema, like what we'd get from json.load()
>>> schema = {
...     "type" : "object",
...     "properties" : {
...         "price" : {"type" : "number"},
...         "name" : {"type" : "string"},
...     },
... }

>>> # If no exception is raised by validate(), the instance is valid.
>>> validate(instance={"name" : "Eggs", "price" : 34.99}, schema=schema)

>>> validate(
...     instance={"name" : "Eggs", "price" : "Invalid"}, schema=schema,
... )                                   
Traceback (most recent call last):
    ...
ValidationError: 'Invalid' is not of type 'number'
Coding boy Hasya

Jawaban yang mirip dengan “Python Jsonschema”

Pertanyaan yang mirip dengan “Python Jsonschema”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya