{
  "name": "天気予報を取得する（失敗を取りこぼさない型）",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "days",
              "triggerAtHour": 7,
              "triggerAtMinute": 0
            }
          ]
        }
      },
      "id": "f7ef9aef-3880-444e-a955-80c547cedea2",
      "name": "毎朝7時",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        96
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "// 取りたい地域をここに並べる。areaCode は気象庁のエリアコード。\n// 失敗側を試したいときは、どれか1件を '999999' にすると 404 になる。\nreturn [\n  { json: { label: '東京', areaCode: '130000' } },\n  { json: { label: '大阪', areaCode: '270000' } },\n  { json: { label: '福岡', areaCode: '400000' } },\n];"
      },
      "id": "55a8c689-711b-486a-9d8c-a9dbaea3487b",
      "name": "取得する地域",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        224,
        96
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "={{ \"https://www.jma.go.jp/bosai/forecast/data/forecast/\" + $json.areaCode + \".json\" }}",
        "authentication": "none",
        "options": {
          "timeout": 20000
        }
      },
      "id": "9d0ffeda-9eae-4268-8990-bfb09a73c5c4",
      "name": "予報を取得",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.5,
      "position": [
        448,
        96
      ],
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "// 気象庁のレスポンスは2要素の配列なので、n8n は1リクエストあたり2アイテムに分ける。\n// 週間予報の方（2件目）には weathers が無いので、ここで落とす。\nconst out = [];\nfor (const item of $input.all()) {\n  const series = item.json.timeSeries;\n  if (!series) continue;\n  const area = series[0].areas[0];\n  if (!area.weathers) continue;\n  out.push({\n    json: {\n      area: area.area.name,\n      publishedAt: item.json.reportDatetime,\n      today: area.weathers[0],\n    },\n  });\n}\nreturn out;"
      },
      "id": "beeb47ab-0ce8-49fa-b2df-c539ebaf8830",
      "name": "今日の天気だけ取り出す",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        672,
        0
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "// エラー出力には、元の入力（label / areaCode）が残ったまま届く。\n// だからどの地域で失敗したのかが分かる。\n// このあとに Slack / Google Sheets などを繋いで記録する。\nreturn $input.all().map(item => ({\n  json: {\n    label: item.json.label,\n    areaCode: item.json.areaCode,\n    httpCode: item.json.details ? item.json.details.httpCode : null,\n    message: item.json.details ? item.json.details.message : String(item.json.error),\n    failedAt: new Date().toISOString(),\n  },\n}));"
      },
      "id": "748cddb7-02ad-456c-9556-a43a0b9b6fff",
      "name": "失敗を1行にまとめる",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        672,
        192
      ]
    }
  ],
  "connections": {
    "毎朝7時": {
      "main": [
        [
          {
            "node": "取得する地域",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "取得する地域": {
      "main": [
        [
          {
            "node": "予報を取得",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "予報を取得": {
      "main": [
        [
          {
            "node": "今日の天気だけ取り出す",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "失敗を1行にまとめる",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
