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
22
PythonでJSON Schemaを扱う
aoken
August 28, 2020
Tweet
Share
Other Decks in Programming
See All in Programming
try! Swift Tokyo 2024のLT枠に採択されたプロポーザルを出すときに考えていたこと
ski
0
350
MicrosoftのPlatform Engineeringガイドを読んで実際になにかやってみた
ymd65536
1
250
HUIT新歓2024「競技プログラミング、やってみませんか?」
slephy2784
1
260
Komplexe Oberflächen mit SVG und der Web Animation API
joergneumann
0
670
今、知っておきたい! 生成AIエージェントの世界
elith
3
350
1BRC--Nerd Sniping the Java Community
gunnarmorling
0
340
try! Swift Tokyo 初参加報告LT
hinakko2
0
210
⼤規模⾔語モデルの拡張(RAG)が 終わったかも知れない件について
nearme_tech
23
15k
Designing for tomorrow's programming workflows
honnibal
PRO
2
120
検証も兼ねて個人開発でHonoとかと向き合った話
hanetsuki
0
330
雑に思考を整理する技術と効能
konifar
58
28k
코틀린으로 멀티플랫폼 만들기
pangmoo
0
140
Featured
See All Featured
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
5
1.5k
Rebuilding a faster, lazier Slack
samanthasiow
72
8.2k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
501
140k
Gamification - CAS2011
davidbonilla
76
4.6k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
20
1.6k
How to Ace a Technical Interview
jacobian
272
22k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
658
120k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
Become a Pro
speakerdeck
PRO
10
4.5k
Visualization
eitanlees
135
14k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
226
16k
jQuery: Nuts, Bolts and Bling
dougneiner
59
7.1k
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`!