📥 サンプルフローJSON(クリックで展開)
[
{
"id": "yaml_sample_tab",
"type": "tab",
"label": "YAML サンプル",
"disabled": false,
"info": ""
},
{
"id": "yaml_comment1",
"type": "comment",
"z": "yaml_sample_tab",
"name": "━━━ YAML → オブジェクト変換 ━━━",
"info": "",
"x": 180,
"y": 40,
"wires": []
},
{
"id": "yaml_inject1",
"type": "inject",
"z": "yaml_sample_tab",
"name": "実行",
"props": [],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"x": 130,
"y": 100,
"wires": [["yaml_template1"]]
},
{
"id": "yaml_template1",
"type": "template",
"z": "yaml_sample_tab",
"name": "YAML設定",
"field": "payload",
"fieldType": "msg",
"format": "yaml",
"syntax": "plain",
"template": "# アプリケーション設定\napp:\n name: My Application\n version: 1.0.0\n debug: true\n\n# データベース設定\ndatabase:\n host: localhost\n port: 5432\n name: mydb\n\n# 機能フラグ\nfeatures:\n - auth\n - logging\n - cache",
"output": "str",
"x": 280,
"y": 100,
"wires": [["yaml_parse"]]
},
{
"id": "yaml_parse",
"type": "yaml",
"z": "yaml_sample_tab",
"property": "payload",
"name": "YAML→Obj",
"x": 430,
"y": 100,
"wires": [["yaml_debug1"]]
},
{
"id": "yaml_debug1",
"type": "debug",
"z": "yaml_sample_tab",
"name": "オブジェクト出力",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"x": 600,
"y": 100,
"wires": []
},
{
"id": "yaml_comment2",
"type": "comment",
"z": "yaml_sample_tab",
"name": "━━━ オブジェクト → YAML変換 ━━━",
"info": "",
"x": 180,
"y": 180,
"wires": []
},
{
"id": "yaml_inject2",
"type": "inject",
"z": "yaml_sample_tab",
"name": "実行",
"props": [],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"x": 130,
"y": 240,
"wires": [["yaml_func1"]]
},
{
"id": "yaml_func1",
"type": "function",
"z": "yaml_sample_tab",
"name": "Docker Compose構造",
"func": "msg.payload = {\n \"version\": \"3.8\",\n \"services\": {\n \"web\": {\n \"image\": \"nginx:latest\",\n \"ports\": [\"80:80\"],\n \"volumes\": [\"./html:/usr/share/nginx/html\"]\n },\n \"db\": {\n \"image\": \"postgres:14\",\n \"environment\": {\n \"POSTGRES_DB\": \"mydb\",\n \"POSTGRES_USER\": \"user\",\n \"POSTGRES_PASSWORD\": \"password\"\n }\n }\n }\n};\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 320,
"y": 240,
"wires": [["yaml_build"]]
},
{
"id": "yaml_build",
"type": "yaml",
"z": "yaml_sample_tab",
"property": "payload",
"name": "Obj→YAML",
"x": 490,
"y": 240,
"wires": [["yaml_debug2"]]
},
{
"id": "yaml_debug2",
"type": "debug",
"z": "yaml_sample_tab",
"name": "docker-compose.yml",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"x": 670,
"y": 240,
"wires": []
},
{
"id": "yaml_comment3",
"type": "comment",
"z": "yaml_sample_tab",
"name": "━━━ 設定値の抽出 ━━━",
"info": "",
"x": 160,
"y": 320,
"wires": []
},
{
"id": "yaml_inject3",
"type": "inject",
"z": "yaml_sample_tab",
"name": "実行",
"props": [],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"x": 130,
"y": 380,
"wires": [["yaml_template2"]]
},
{
"id": "yaml_template2",
"type": "template",
"z": "yaml_sample_tab",
"name": "センサー設定YAML",
"field": "payload",
"fieldType": "msg",
"format": "yaml",
"syntax": "plain",
"template": "sensors:\n temperature:\n pin: 4\n interval: 5000\n threshold:\n min: 10\n max: 40\n humidity:\n pin: 5\n interval: 10000\n threshold:\n min: 30\n max: 80",
"output": "str",
"x": 310,
"y": 380,
"wires": [["yaml_parse2"]]
},
{
"id": "yaml_parse2",
"type": "yaml",
"z": "yaml_sample_tab",
"property": "payload",
"name": "YAML→Obj",
"x": 470,
"y": 380,
"wires": [["yaml_func2"]]
},
{
"id": "yaml_func2",
"type": "function",
"z": "yaml_sample_tab",
"name": "温度センサー設定抽出",
"func": "var tempConfig = msg.payload.sensors.temperature;\nmsg.payload = {\n sensor: \"temperature\",\n gpioPin: tempConfig.pin,\n readInterval: tempConfig.interval,\n alertMin: tempConfig.threshold.min,\n alertMax: tempConfig.threshold.max\n};\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 660,
"y": 380,
"wires": [["yaml_debug3"]]
},
{
"id": "yaml_debug3",
"type": "debug",
"z": "yaml_sample_tab",
"name": "温度設定",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": true,
"complete": "payload",
"targetType": "msg",
"statusVal": "payload.readInterval",
"statusType": "msg",
"x": 860,
"y": 380,
"wires": []
}
]
演習3: ネストした設定の取得 中級
📋 課題: ネストしたYAML設定から特定の値を抽出してください。
✅ 成功の条件:
- デバッグパネルにオブジェクト型のpayloadが表示される
host プロパティに "db1.example.com"、port プロパティに 5432(数値型)が入っている
connectionString プロパティに "db1.example.com:5432" が入っている
- ノードのステータス表示(ノード下部)に接続文字列が表示されている
💡 ヒント
Change ノードの「値の代入」で、型を「JSONata」に設定し、payload.database.primary のようにネストしたプロパティにアクセスします。JSONata式で文字列連結には & 演算子を使います。
✅ 解答例フロー
[
{"id": "ex3_tab", "type": "tab", "label": "演習3"},
{"id": "ex3_inject", "type": "inject", "z": "ex3_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 100, "wires": [["ex3_template"]]},
{"id": "ex3_template", "type": "template", "z": "ex3_tab", "name": "設定YAML", "field": "payload", "fieldType": "msg", "format": "yaml", "syntax": "plain", "template": "database:\n primary:\n host: db1.example.com\n port: 5432\n replica:\n host: db2.example.com\n port: 5432", "output": "str", "x": 280, "y": 100, "wires": [["ex3_yaml"]]},
{"id": "ex3_yaml", "type": "yaml", "z": "ex3_tab", "property": "payload", "name": "YAML→Obj", "x": 430, "y": 100, "wires": [["ex3_change"]]},
{"id": "ex3_change", "type": "change", "z": "ex3_tab", "name": "Primary DB取得", "rules": [{"t": "set", "p": "payload", "pt": "msg", "to": "{\"host\": payload.database.primary.host, \"port\": payload.database.primary.port, \"connectionString\": payload.database.primary.host & \":\" & $string(payload.database.primary.port)}", "tot": "jsonata"}], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 600, "y": 100, "wires": [["ex3_debug"]]},
{"id": "ex3_debug", "type": "debug", "z": "ex3_tab", "name": "DB設定", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload.connectionString", "statusType": "msg", "x": 770, "y": 100, "wires": []}
]
演習4: 配列を含むYAML 中級
📋 課題: 配列を含むYAMLを解析し、配列の要素数と内容を取得してください。
✅ 成功の条件:
- デバッグパネルにオブジェクト型のpayloadが表示される
totalUsers プロパティに 3(数値型)が入っている
admins プロパティが配列型で、["田中"] が入っている
allNames プロパティが配列型で、["田中", "鈴木", "佐藤"] が入っており、ノードのステータスに 3 が表示されている
💡 ヒント
Change ノードの「値の代入」で、型を「JSONata」に設定します。配列の要素数は $count() 関数、フィルタリングは [role="admin"] のようなフィルタ式、名前の抽出は .name で取得できます。
✅ 解答例フロー
[
{"id": "ex4_tab", "type": "tab", "label": "演習4"},
{"id": "ex4_inject", "type": "inject", "z": "ex4_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 100, "wires": [["ex4_template"]]},
{"id": "ex4_template", "type": "template", "z": "ex4_tab", "name": "ユーザーYAML", "field": "payload", "fieldType": "msg", "format": "yaml", "syntax": "plain", "template": "users:\n - name: 田中\n role: admin\n - name: 鈴木\n role: editor\n - name: 佐藤\n role: viewer", "output": "str", "x": 290, "y": 100, "wires": [["ex4_yaml"]]},
{"id": "ex4_yaml", "type": "yaml", "z": "ex4_tab", "property": "payload", "name": "YAML→Obj", "x": 450, "y": 100, "wires": [["ex4_change"]]},
{"id": "ex4_change", "type": "change", "z": "ex4_tab", "name": "ユーザー情報", "rules": [{"t": "set", "p": "payload", "pt": "msg", "to": "{\"totalUsers\": $count(payload.users), \"admins\": payload.users[role=\"admin\"].name, \"allNames\": payload.users.name}", "tot": "jsonata"}], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 610, "y": 100, "wires": [["ex4_debug"]]},
{"id": "ex4_debug", "type": "debug", "z": "ex4_tab", "name": "ユーザー統計", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload.totalUsers", "statusType": "msg", "x": 790, "y": 100, "wires": []}
]