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
JSONataを使ってみよう Step Functionsが楽しくなる実践テクニック #dev...
Search
dafujii
September 03, 2025
Programming
1
870
JSONataを使ってみよう Step Functionsが楽しくなる実践テクニック #devio2025
DevelopersIO 2025 Osaka 登壇資料です。
dafujii
September 03, 2025
Tweet
Share
More Decks by dafujii
See All by dafujii
Software Design 定期購読のススメ
dafujii
1
4.2k
100均商品でスモールスタートを決めよう MixLeap Live LT #42 - 「デスク周りのDIY」
dafujii
0
1k
オフィスランチにこそ完全食
dafujii
0
2.5k
石油王よりもOSSコントリビュータを選んだ話
dafujii
0
710
チームサンドイッチマン ゼロカロリースキル #Alexaスキルアワード 2019
dafujii
1
130
Other Decks in Programming
See All in Programming
Migration to Signals, Resource API, and NgRx Signal Store
manfredsteyer
PRO
0
140
CSC509 Lecture 08
javiergs
PRO
0
270
Blazing Fast UI Development with Compose Hot Reload (droidcon London 2025)
zsmb
0
430
SODA - FACT BOOK(JP)
sodainc
1
9k
Swift Concurrency 年表クイズ
omochi
3
210
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
110
Pythonに漸進的に型をつける
nealle
1
140
業務でAIを使いたい話
hnw
0
220
Reactive Thinking with Signals and the Resource API
manfredsteyer
PRO
0
120
CSC305 Lecture 11
javiergs
PRO
0
320
3年ぶりにコードを書いた元CTOが Claude Codeと30分でMVPを作った話
maikokojima
0
710
Blazing Fast UI Development with Compose Hot Reload (Bangladesh KUG, October 2025)
zsmb
2
440
Featured
See All Featured
Automating Front-end Workflow
addyosmani
1371
200k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
GitHub's CSS Performance
jonrohan
1032
470k
It's Worth the Effort
3n
187
28k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.1k
Reflections from 52 weeks, 52 projects
jeffersonlam
355
21k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.7k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
650
Fireside Chat
paigeccino
41
3.7k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.1k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.1k
Facilitating Awesome Meetings
lara
57
6.6k
Transcript
JSONataを使ってみよう Step Functionsが楽しく なる実践テクニック ふじい 製造ビジネステクノロジー部 スマートプロダクトチーム
2 • 2020年9⽉ ⼊社 ◦ サーバサイドエンジニアのお仕事 • 2023年3⽉ 育児休業取得 ◦ 育休⼀年取ってみた 〜4カ⽉⽬〜
◦ 育休⼀年取ってみた 〜13カ⽉⽬〜 • 2024年5⽉ 育児休業から復帰 • 2025年9⽉ ◦ DevelopersIO 2025 Osaka 登壇なう ◦ ⼊社してもう5年経ってる怖 部署 • 製造ビジネステクノロジー部 近況 • マリカーワールドで全キャラ、 全コスチューム解放完了! 資格: AWS 全部 All Cert: 2022, 2023, 2024, 2025 自己紹介 ふじい
今⽇話す内容について 3 ターゲット • まだ JSONPath を使っている⼈ • これから Step
Functions を触ろうとしている⼈ ゴール • Step Functions で JSONata を使ってみる
JSONata はいいぞ
【悲報】re:Growth 2024 OSAKAでJSONataについて話されていた 5 【朗報】使いまわせる https://dev.classmethod.jp/articles/re-growth-2024-osaka-jsonata/
6 https://dev.classmethod.jp/articles/re-growth-2024-osaka-jsonata/
7 https://dev.classmethod.jp/articles/re-growth-2024-osaka-jsonata/
8 https://dev.classmethod.jp/articles/re-growth-2024-osaka-jsonata/
9 https://dev.classmethod.jp/articles/re-growth-2024-osaka-jsonata/
10 https://dev.classmethod.jp/articles/re-growth-2024-osaka-jsonata/
JSONata で Fizz Buzz やってみた
1.各ステートで利⽤ 12 ステートマシンらしい⾒た⽬
1. 各ステートで利⽤ 13 "Items": "{% [1..1000] %}", "Choices": [ {
"Condition": "{% $states.input%15=0 %}", "Next": "15" },(略) ], "Default": "Default" "Output":{"output":"{% $states.input^(number).output %}"} "Output": { "number": "{% $states.input %}", "output": "{% $states.input %}" }
1. 各ステートで利⽤ 14 実⾏時間: 約26秒 状態遷移数: 2004
1. 各ステートで利⽤ 15 実⾏時間: 約26秒 状態遷移数: 2004
1. 各ステートで利⽤ 16 教訓 ループに気をつけよう (JSONata関係なく)
2. 1ステートで完結 17 これぞ JSONata ステートマシン 超シンプル
2. 1ステートで完結 18 { "output": "{% $map([1..1000], function($i) {\n $i
% 15 = 0 ? \"FizzBuzz\" :\n $i % 3 = 0 ? \"Fizz\" :\n $i % 5 = 0 ? \"Buzz\" :\n ($i)\n})\n %}" } $map([1..1000], function($i) { $i % 15 = 0 ? "FizzBuzz" : $i % 3 = 0 ? "Fizz" : $i % 5 = 0 ? "Buzz" : ($i) }) ※ダブルクオーテーションはエスケープ されるのでシングルクォーテーションの 使⽤をおすすめします。
2. 1ステートで完結 19 実⾏時間: 約0.1秒 状態遷移数: 3 めっちゃ速い! 課⾦も抑えられる!
JSONata にわくわくしてきました? • プログラミング構造はチューリング完全 • ループ処理を1ステート内で表現できれば、状態遷移数が節約できる • Lambda でしていた処理を表現できれば、処理時間の短縮ができる =>
つまり 料⾦の削減、処理時間の削減 ができる • AI に「⚪⚪の処理を JSONata で書いて」が通じる • (あと書いててちょっと楽しい) 20
JSONata で⽇付操作やってみた
和暦変換 • 22 https://dev.classmethod.jp/articles/step-functions-jsonata-japanese-era-date-conversion/
和暦変換 ($toWareki := function($year, $month, $day){( $eras := [ {"name":"令和","abbr":"R","start":{"year":2019,"month":5,"day":1}},
{"name":"平成","abbr":"H","start":{"year":1989,"month":1,"day":8}}, {"name":"昭和","abbr":"S","start":{"year":1926,"month":12,"day":25}}, {"name":"⼤正","abbr":"T","start":{"year":1912,"month":7,"day":30}}, {"name":"明治","abbr":"M","start":{"year":1868,"month":1,"day":25}}]; $compareDate := function($y1, $m1, $d1, $y2, $m2, $d2) { $y1 > $y2 ? 1 : $y1 < $y2 ? -1 : $m1 > $m2 ? 1 : $m1 < $m2 ? -1 : $d1 > $d2 ? 1 : $d1 < $d2 ? -1 : 0 }; $era := $eras[$compareDate($year, $month, $day, start.year, start.month, start.day) >= 0 ][0]; $era = null ? { "wareki": "不明", "warekiAbbr": "?", "year": $year } : ( $eraYear := $year - $era.start.year + 1; $displayYear := $eraYear = 1 ? "元" : $eraYear; { "wareki": $era.name & $displayYear & "年", "warekiAbbr": $era.abbr & $displayYear, "year": $year, "month": $month, "day": $day } ) )}; $toWareki($states.input.year, $states.input.month, $states.input.day);) 23
うるう年判定 24 $fromMillis( $toMillis( $states.input.year & "-02-28T00:00:00" ) + 60*60*24*1000,
"[M1]" ) = "2"
JSONata で ⚪⚪ やってみた
DevelopersIO で JSONata タグの記事を⾒てね 26 https://dev.classmethod.jp/tags/jsonata/
注意点
テストがしにくい 28 CDK で Step Functions + Lambda の場合 •
Lambda 関数のコードに対して単体テストが書きやすい でも JSONata のテストとなると……
テストがしにくい 29 CDK で Step Functions で JSONata ステートのテストの場合 •
変更するたびにデプロイして、マネコンでポチポチはつらい • LocalStack は Step Functions の JSONata 対応済み ◦ ※ 無料版に Step Functions が含まれている 🙌 ◦ Lambda 関数のテスト環境構築するより⼿間 • テストを記述しようとすると、AWS SDK で StartExecution API や TestState API を叩くテストになる(統合テスト)
Exerciser と Step Functions で挙動が異なる 30 $toMillis("2025-02-29") • JSONata Exerciser
は動く: 1677628800000 (3/1) • Step Functions だとエラーになる ◦ うるう年(2024-02-29)、うるう⽇以外(2023-02-28)なら成功 $toMillis("2025-02-29T00:00:00") • 時間まで記載すればStep Functions でエラーにならない • ただし JSONata Exerciser とは結果が異なる ◦ ブラウザのタイムゾーンが使⽤されるため
うるう年判定(再) 31 $fromMillis($toMillis("2023-02-29T00:00:00"), "[M1]") = "2" • 2/29 をタイムスタンプに変換して再度⽇付に変換し2⽉ならうるう年 •
JSONata Exerciser ならこれだけでいい • Step Functions だとなぜかタイムスタンプ変換後 2/28 になる $fromMillis($toMillis("2023-02-28T00:00:00")+60*60*24*1000,"[M1]")="2" • 2/28 の翌⽇が2⽉ならうるう年 • Step Functions で動かそうとするとこうする必要があった • JSONata Exerciser だとブラウザのタイムゾーンを使⽤するため常 true
うるう年判定(再) 32 Step Functions でも JSONata Execiser でも動かすには • $fromMillis($toMillis("2023-02-28T00:00:00Z")+60*60*24*1000,
"[M1]") = "2" • ⽇付だけではなく時分秒まで記述する • タイムゾーンが UTC であると Z を明記する • 2/29 ではなく、 2/28 + 1⽇をタイムスタンプに変換する • JSONata Exerciser で動いても Step Functions で動くとは限らない
まとめ
JSONata はいいぞ 34 使ってみよう • JSON をクエリするだけでなく、計算や⽇付操作など処理ができる • JSONata Exerciser
で気軽に試せる ◦ でも JSONata Exerciser で動いても、Step Functions で動かない パターンがある ◦ JSONata を試す⽤のステートマシンを作っておくと便利
JSONata はいいぞ 35 でも効果的に使おう • うまく使えば料⾦削減、実⾏時間削減できる • テスタビリティが低いので使い所を⾒極めよう JSON の加⼯に徹する、変更の少ない箇所、
組み込み関数を有効活⽤しプログラミングを頑張らない ⾮ビジネスロジック、Lambda を使うほどでもない処理、 既存の JSONPath の Step Function は無理に移⾏しない、など ぜひ使ってみてください!
None