Hi im having json structure as follows:
{
"datasetType": "monolingual-corpus",
"languages": {
"sourceLanguage": "hi"
},
"collectionSource": [
"http://pib.gov.in/"
],
"domain":[
"news"
],
"license": "cc-by-4.0",
"submitter": {
"name": "Project aroad",
"aboutMe": "Open source project run by aroad foundation",
"team": [
{
"name": "Navneet Kumar hegde",
"aboutMe": "NLP team lead at Project aroad"
},
{
"name": "Aswini Pradeep",
"aboutMe": "Backend team lead at Project aroad"
}
]
}
I can validate datasetType only using json schema. How can i validate other values such as "languages", "collectoinsource", "submitter". In "Submitter" all fields should be mandatory and how to validate "Team" inside "submitter"
Code i have written in python is only validating "datasetType", not able to validate remaining fields. please help me regarding this, Thanks in advance
try marshmallow
. It's great for validating schemas.
from marshmallow import Schema, fields
class LanguageSchema(Schema):
sourceLanguage = fields.String(required=True)
class UserSchema(Schema):
name = fields.String(required=True)
aboutMe = fields.String(required=True)
class SubmitterSchema(UserSchema):
team = fields.List(fields.Nested(UserSchema()))
class ExampleSchema(Schema):
datasetType = fields.String(required=True)
languages = fields.Nested(LanguageSchema(), required=True)
collectionSource = fields.List(fields.URL, required=True)
domain = fields.List(fields.String(), required=True)
license = fields.String(required=True)
submitter = fields.Nested(SubmitterSchema(), required=True)
data = {
"datasetType": "monolingual-corpus",
"languages": {
"sourceLanguage": "hi"
},
"collectionSource": [
"http://pib.gov.in/"
],
"domain": [
"news"
],
"license": "cc-by-4.0",
"submitter": {
"name": "Project aroad",
"aboutMe": "Open source project run by aroad foundation",
"team": [
{
"name": "Navneet Kumar hegde",
"aboutMe": "NLP team lead at Project aroad"
},
{
"name": "Aswini Pradeep",
"aboutMe": "Backend team lead at Project aroad"
}
]
}
}
# initialize schema
schema = ExampleSchema()
# validate data, will throw error if data does not fit schema
validated_data = schema.load(data)
You can use a standard called JSON Schema and a python lib jsonschema
to validate json data with your own schema.https://python-jsonschema.readthedocs.io/en/stable/validate/
As for writing the schema and validating what you want, you have a couple of ways to go about it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.