Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
PythonでJSON Schemaを扱う
Search
aoken
August 28, 2020
Programming
0
64
PythonでJSON Schemaを扱う
aoken
August 28, 2020
Tweet
Share
Other Decks in Programming
See All in Programming
AIのバカさ加減に怒る前にやっておくこと
blueeventhorizon
0
120
Designing Repeatable Edits: The Architecture of . in Vim
satorunooshie
0
150
ボトムアップの生成AI活用を推進する社内AIエージェント開発
aku11i
0
1.2k
スキーマ駆動で、Zod OpenAPI Honoによる、API開発するために、Hono Takibiというライブラリを作っている
nakita628
0
330
Google Opalで使える37のライブラリ
mickey_kubo
3
170
data-viz-talk-cz-2025
lcolladotor
0
100
iOSでSVG画像を扱う
kishikawakatsumi
0
180
コードとあなたと私の距離 / The Distance Between Code, You, and I
hiro_y
0
210
エンジニアインターン「Treasure」とHonoの2年、そして未来へ / Our Journey with Hono Two Years at Treasure and Beyond
carta_engineering
0
460
Kotlinで実装するCPU/GPU 「協調的」パフォーマンス管理
matuyuhi
0
100
Dive into Triton Internals
appleparan
0
270
マンガアプリViewerの大画面対応を考える
kk__777
0
420
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.2k
Java REST API Framework Comparison - PWX 2021
mraible
34
8.9k
Why Our Code Smells
bkeepers
PRO
340
57k
Facilitating Awesome Meetings
lara
57
6.6k
Product Roadmaps are Hard
iamctodd
PRO
55
11k
Testing 201, or: Great Expectations
jmmastey
45
7.7k
Optimizing for Happiness
mojombo
379
70k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.7k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
RailsConf 2023
tenderlove
30
1.3k
Practical Orchestrator
shlominoach
190
11k
Transcript
PythonでJSON Schemaを扱う PyCon 2020 ビザスク スポンサーブース LT Kenya Aono
お前、誰よ 青野 剣矢(Aono Kenya) - ビザスク フルサポート開発チーム所属 - Python, Django
- Typescript, Vue.js, AngularJS
このLTのアジェンダ - JSON Schemaってなんだ? - PythonでJSON Schemaを活用してみる
JSON Schemaってなんだ?
JSON Schemaとは - JSONの構造を定義するための規格 - APIの仕様書として使ったり - HTTP Content-type application/json
のバリデーションとして使った り - 様々なプログラミング言語で扱うことができる
JSON Schemaを定義してみる { "name": "Kenya Aono", "age": 29, "team": "fullsupport-dev"
"startDate": "2019-04-01" }
{ "$schema": "http://json-schema.org/draft-07/schema", "type": "object", "properties": { "name": { "type":
"string" }, "age": { "type": "integer" }, "team": { "type": "string" }, "startDate": { "type": "string" } } }
enumで選択肢を定義する "team": { "type": "string", "enum": [ "fullsuport-dev", "advisor-dev", "lite-dev"
] },
正規表現でパターンを定義する "startDate": { "type": "string", "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" }
PythonでJSON Schemaを扱う
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)
{ "$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" } } }
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'
まとめ - JSON Schemaを使えばJSONの構造を明確化することができる - PythonでJSON Schemaを扱いたいなら`pip install jsonschema`!