🎣
Salmonia3+のJSONをパースしてみる
スプラトゥーン3のサーモンランの結果のJSONを分析するために久しぶりにjq
を使ったのでメモ。
JSONはSalmonia3+から取得。
% jq --version
jq-1.6
JSON全体を見つつ、keys
で構造を理解していく。
% jq 'keys' salmonia_20230415173128.json
[
"schedules",
"signature",
"version"
]
% jq '.schedules | keys' salmonia_20230415173128.json
[
0,
1,
2,
3,
4,
5
]
% jq '.schedules[0] | keys' salmonia_20230415173128.json
[
"end_time",
"id",
"mode",
"rare_weapon",
"results",
"rule",
"stage_id",
"start_time",
"weapon_list"
]
# プレイしていないスケジュールの配列だと空なので、データが入っている配列を指定している
% jq '.schedules[5].results[0] | keys' salmonia_20230415173128.json
[
"boss_counts",
"boss_id",
"boss_kill_counts",
"danger_rate",
"failure_wave",
"golden_ikura_assist_num",
"golden_ikura_num",
"grade_id",
"grade_point",
"id",
"ikura_num",
"is_boss_defeated",
"is_clear",
"job_bonus",
"job_rate",
"job_score",
"kuma_point",
"npln_user_id",
"play_time",
"players",
"salmon_id",
"scale",
"scenario_code",
"smell_meter",
"uuid",
"waves"
]
欲しい部分を抽出する。
% jq '.schedules[5].results[] | {grade_point, danger_rate, play_time}' salmonia_20230415173128.json
{
"grade_point": 880,
"danger_rate": 3.33,
"play_time": "2023-04-14T15:21:14Z"
}
{
"grade_point": 780,
"danger_rate": 3.12,
"play_time": "2023-04-14T13:04:15Z"
}
.
.
play_time
の昇順でソートする。
% jq '.schedules[5].results[] | {grade_point, danger_rate, play_time}' salmonia_20230415173128.json | jq -s 'sort_by(.play_time)'
[
{
"grade_point": 750,
"danger_rate": 3.12,
"play_time": "2023-04-14T06:53:46Z"
},
{
"grade_point": 770,
"danger_rate": 3.05,
"play_time": "2023-04-14T07:02:10Z"
},
.
.
表計算アプリケーションでグラフを描きたかったのでCSV形式にする。
% jq '.schedules[5].results[] | {grade_point, danger_rate, play_time}' salmonia_20230415173128.json | jq -s 'sort_by(.play_time) | .[] | [.play_time, .danger_rate, .grade_point]' | jq -rs '.[] | @csv'
"2023-04-14T06:53:46Z",3.12,750
"2023-04-14T07:02:10Z",3.05,770
"2023-04-14T07:09:10Z",3.09,760
.
.
参考
- https://dev.classmethod.jp/articles/road-to-jq-master-novice/
- https://lzone.de/cheat-sheet/jq
- https://qiita.com/yoza/items/c4c732779a5a10fd3b4c
- https://developer.zendesk.com/documentation/integration-services/developer-guide/jq-cheat-sheet/
- https://qiita.com/eielh/items/aff045e1689be8e89972
- https://medium.com/veltra-engineering/jq-supports-json-to-csv-fb5c951a9575
Discussion