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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
aoken
August 28, 2020
Programming
0
66
PythonでJSON Schemaを扱う
aoken
August 28, 2020
Tweet
Share
Other Decks in Programming
See All in Programming
20260315 AWSなんもわからん🥲
chiilog
2
180
Tamach-sre-3_ANDPAD-shimaison93
mane12yurks38
0
190
Rethinking API Platform Filters
vinceamstoutz
0
3k
へんな働き方
yusukebe
6
2.9k
Takumiから考えるSecurity_Maturity_Model.pdf
gessy0129
1
170
Geminiをパートナーに神社DXシステムを個人開発した話(いなめぐDX 開発振り返り)
fujiba
0
120
PHP でエミュレータを自作して Ubuntu を動かそう
m3m0r7
PRO
2
150
How to stabilize UI tests using XCTest
akkeylab
0
150
Java 21/25 Virtual Threads 소개
debop
0
300
Strategy for Finding a Problem for OSS: With Real Examples
kibitan
0
120
Fundamentals of Software Engineering In the Age of AI
therealdanvega
2
300
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
1.1k
Featured
See All Featured
How STYLIGHT went responsive
nonsquared
100
6k
How Software Deployment tools have changed in the past 20 years
geshan
0
33k
Context Engineering - Making Every Token Count
addyosmani
9
780
Optimizing for Happiness
mojombo
378
71k
Discover your Explorer Soul
emna__ayadi
2
1.1k
Accessibility Awareness
sabderemane
0
87
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
400
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
91
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
The Pragmatic Product Professional
lauravandoore
37
7.2k
Side Projects
sachag
455
43k
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`!