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
61
PythonでJSON Schemaを扱う
aoken
August 28, 2020
Tweet
Share
Other Decks in Programming
See All in Programming
The Modern View Layer Rails Deserves: A Vision For 2025 And Beyond @ RailsConf 2025, Philadelphia, PA
marcoroth
2
530
技術同人誌をMCP Serverにしてみた
74th
1
670
型で語るカタ
irof
0
200
テストから始めるAgentic Coding 〜Claude Codeと共に行うTDD〜 / Agentic Coding starts with testing
rkaga
13
4.9k
ruby.wasmで多人数リアルタイム通信ゲームを作ろう
lnit
3
500
Node-RED を(HTTP で)つなげる MCP サーバーを作ってみた
highu
0
120
Advanced Micro Frontends: Multi Version/ Framework Scenarios @WAD 2025, Berlin
manfredsteyer
PRO
0
320
ISUCON研修おかわり会 講義スライド
arfes0e2b3c
1
450
GPUを計算資源として使おう!
primenumber
1
180
ふつうの技術スタックでアート作品を作ってみる
akira888
1
910
Railsアプリケーションと パフォーマンスチューニング ー 秒間5万リクエストの モバイルオーダーシステムを支える事例 ー Rubyセミナー 大阪
falcon8823
5
1.2k
PHPで始める振る舞い駆動開発(Behaviour-Driven Development)
ohmori_yusuke
2
410
Featured
See All Featured
Music & Morning Musume
bryan
46
6.6k
Done Done
chrislema
184
16k
Fireside Chat
paigeccino
37
3.5k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
[RailsConf 2023] Rails as a piece of cake
palkan
55
5.7k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
48
2.9k
Testing 201, or: Great Expectations
jmmastey
43
7.6k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Art, The Web, and Tiny UX
lynnandtonic
299
21k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
45
7.5k
Gamification - CAS2011
davidbonilla
81
5.4k
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`!