Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PythonでJSON Schemaを扱う

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for aoken aoken
August 28, 2020

PythonでJSON Schemaを扱う

Avatar for aoken

aoken

August 28, 2020
Tweet

Other Decks in Programming

Transcript

  1. JSON Schemaとは - JSONの構造を定義するための規格 - APIの仕様書として使ったり - HTTP Content-type application/json

    のバリデーションとして使った り - 様々なプログラミング言語で扱うことができる
  2. { "$schema": "http://json-schema.org/draft-07/schema", "type": "object", "properties": { "name": { "type":

    "string" }, "age": { "type": "integer" }, "team": { "type": "string" }, "startDate": { "type": "string" } } }
  3. pip install jsonschema from jsonschema import validate, ValidationError import schema

    # さっき定義したスキーマとします data = { 'name': 'Kenya Aono', 'age': 29, 'team': 'fullsuport-dev', 'startDate': '2019-04-01', } try: validate(data, schema) except ValidationError as e: print(e.message)
  4. { "$schema": "http://json-schema.org/draft-07/schema", "type": "object", "required": [ "name", "age", "gender"

    ], "properties": { "name": { "type": "string" }, "age": { "type": "integer" }, "team": { "type": "string" }, "startDate": { "type": "string" } } }
  5. jsonschema data = { 'name': 'Kenya Aono', 'age': '29sai', 'team':

    'fullsuport-dev', 'startDate': '2019-04-01', } try: validate(data, schema) except ValidationError as e: print(e.message) Invalid JSON - 'age' is not of type 'integer'