Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JSONataを使ってみよう Step Functionsが楽しくなる実践テクニック #dev...

Avatar for dafujii dafujii
September 03, 2025

JSONataを使ってみよう Step Functionsが楽しくなる実践テクニック #devio2025

DevelopersIO 2025 Osaka 登壇資料です。

Avatar for dafujii

dafujii

September 03, 2025
Tweet

More Decks by dafujii

Other Decks in Programming

Transcript

  1. 2 • 2020年9⽉ ⼊社 ◦ サーバサイドエンジニアのお仕事 • 2023年3⽉ 育児休業取得 ◦ 育休⼀年取ってみた 〜4カ⽉⽬〜

    ◦ 育休⼀年取ってみた 〜13カ⽉⽬〜 • 2024年5⽉ 育児休業から復帰 • 2025年9⽉ ◦ DevelopersIO 2025 Osaka 登壇なう ◦ ⼊社してもう5年経ってる怖 部署 • 製造ビジネステクノロジー部 近況 • マリカーワールドで全キャラ、 全コスチューム解放完了! 資格: AWS 全部 All Cert: 2022, 2023, 2024, 2025 自己紹介 ふじい
  2. 今⽇話す内容について 3 ターゲット • まだ JSONPath を使っている⼈ • これから Step

    Functions を触ろうとしている⼈ ゴール • Step Functions で JSONata を使ってみる
  3. 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 %}" }
  4. 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) }) ※ダブルクオーテーションはエスケープ されるのでシングルクォーテーションの 使⽤をおすすめします。
  5. 和暦変換 ($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
  6. テストがしにくい 28 CDK で Step Functions + Lambda の場合 •

    Lambda 関数のコードに対して単体テストが書きやすい でも JSONata のテストとなると……
  7. テストがしにくい 29 CDK で Step Functions で JSONata ステートのテストの場合 •

    変更するたびにデプロイして、マネコンでポチポチはつらい • LocalStack は Step Functions の JSONata 対応済み ◦ ※ 無料版に Step Functions が含まれている 🙌 ◦ Lambda 関数のテスト環境構築するより⼿間 • テストを記述しようとすると、AWS SDK で StartExecution API や TestState API を叩くテストになる(統合テスト)
  8. 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 とは結果が異なる ◦ ブラウザのタイムゾーンが使⽤されるため
  9. うるう年判定(再) 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
  10. うるう年判定(再) 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 で動くとは限らない
  11. JSONata はいいぞ 34 使ってみよう • JSON をクエリするだけでなく、計算や⽇付操作など処理ができる • JSONata Exerciser

    で気軽に試せる ◦ でも JSONata Exerciser で動いても、Step Functions で動かない パターンがある ◦ JSONata を試す⽤のステートマシンを作っておくと便利
  12. JSONata はいいぞ 35 でも効果的に使おう • うまく使えば料⾦削減、実⾏時間削減できる • テスタビリティが低いので使い所を⾒極めよう JSON の加⼯に徹する、変更の少ない箇所、

    組み込み関数を有効活⽤しプログラミングを頑張らない ⾮ビジネスロジック、Lambda を使うほどでもない処理、 既存の JSONPath の Step Function は無理に移⾏しない、など ぜひ使ってみてください!