JSON Values into Power Automate
2019/12/07
Logicflow-ja
小尾 智之
関心分野: NoCode/LowCode開発
コミュニティ: LogicFlow-ja / CLR/H
Microsoft MVP for Microsoft Azure(2017.03 ~)
@twit_ahf
tomoyuki.obi
VB / PowerPlatform
https://blog.kuma.icu
Enterprise Integration
小尾 智之(Tomoyuki Obi)
https://el.jibun.atmarkit.co.jp/ahf/
自己紹介
風物詩
今回のお題目
•JSON しか喋らないぞ、覚悟しろ
What is “JSON” ?
JSON とは
• JavaScript Object Notation の略
• 気が付いたら広く存在していたもの
(1996年 Netscape Navigator で利用された形跡あり)
• JavaScriptにおけるオブジェクト表記法のサブセット
• 各種団体で標準として定義
RFC 8259 / IETF STD 90 / ECMA-404 2nd edition
JSON での記述
• 数値
• 文字列
• 真偽値(true / false)
• 配列
• オブジェクト
• null
利用できる値の種類 [ “Power Automate”, “Power Apps”,
”Power BI”,”Power Virtual Agents” ]
配列の記述
[ ] でくくると配列となる
オブジェクトの記述
{
“servicename”: “Power Automate”,
“expriredate”: null
},
{
“servicename”: “Microsoft Flow”,
“expriredate”: “2019-11-01T00:00:00Z”
}
文字列でキー名
: の後に値を記述
UTF-8 であることが必須
インデントは問わない
Power Automate での JSON
HTTP Request トリガや
何かしらの HTTP アクセスで得た結果を扱う際にJSONに触れることが多い
How to befriend “JSON”
JSON データに対してできること
JSON データの入出力時に行える対応
• スキーマによるデータ検証
• 値の取得
• 値のコピー
値の変更はできない
JSON スキーマの作成
「要求:HTTP Request」トリガ
「応答:HTTP Response」アクション
または
「JSONの解析」アクション
元とするデータがある場合は
そのデータからスキーマを生成できる
以前と異なり Required 指定がつくことは減った
応答:HTTP Response アクションで設定したスキーマ情報は
「子フローの実行」アクション利用時に
呼び出し元のワークフローから自動で参照される
{
“description”: “JSONスキーマを検証するサンプル2”,
“type”: “object”,
“properties”: {
“other1”: {
“type”: “string”,
“enum”: [
“value1”,
“value2”,
“value3”
]
},
“other2”: {
“type”: “string”
},
"other3": {
"type": "string"
}
},
"required": [
"other1"
]
}
「要求:HTTP Request」トリガでの
スキーマ設定は呼び出し側の入力項目を
ある程度制御できる
「要求:HTTP Request」トリガ
でのスキーマ検証オプションをオンにすると
設定したスキーマに一致しないデータを伴った
呼び出しをエラーとしてはじくことが可能
JSON データの参照
{
"eventDataSchemaVersion": "1.0",
"schemaVersion": "1.0",
"correlationId": "bddc4200-ebef-4c70-8430-a4a55bf22f8c",
"clientSessionId": "",
"clientRequestId": "",
"component": "ServerHost",
"eventType": "NotSpecified",
"traceLevel": "Info",
"operationName": "RpaWebServer.Start",
"eventTimestamp": "2019-11-25T01:27:39.3100838Z",
"message": "Listening on 'http://localhost:4723/'",
"eventData": {
"osInfo": {
"name": "Microsoft Windows NT 6.2.9200.0",
"version": "6.2.9200.0",
"arch": "Win32NT"
},
"machineInfo": {
"screenResolution": "1920x1200",
"nbCores": "8",
"netversion": "4.0.30319.42000"
}
}
}
トリガで左のような JSON を受信
その後のアクションで値を参照したい場合
{
"eventDataSchemaVersion": "1.0",
"message": "Listening on 'http://localhost:4723/'",
"eventData": {
"osInfo": {
"name": "Microsoft Windows NT 6.2.9200.0",
"version": "6.2.9200.0",
"arch": "Win32NT"
},
"machineInfo": {
"screenResolution": "1920x1200",
"nbCores": "8",
"netversion": "4.0.30319.42000"
}
}
}
JSON データの階層
triggerBody()
triggerBody().eventDataSchemaVersion
triggerBody().eventData.osInfo
triggerBody().eventData.osInfo.version
{
"eventDataSchemaVersion": "1.0",
"message": "Listening on 'http://localhost:4723/'",
"eventData": {
"osInfo": {
"name": "Microsoft Windows NT 6.2.9200.0",
"version": "6.2.9200.0",
"arch": "Win32NT"
},
"machineInfo": {
"screenResolution": "1920x1200",
"nbCores": "8",
"netversion": "4.0.30319.42000"
}
}
}
JSON データの階層
triggerBody()
triggerBody().eventDataSchemaVersion
triggerBody().eventData.osInfo
triggerBody().eventData.osInfo.version
{
"eventDataSchemaVersion": "1.0",
"message": "Listening on 'http://localhost:4723/'",
"eventData": {
"osInfo": {
"name": "Microsoft Windows NT 6.2.9200.0",
"version": "6.2.9200.0",
"arch": "Win32NT"
},
"machineInfo": {
"screenResolution": "1920x1200",
"nbCores": "8",
"netversion": "4.0.30319.42000"
}
}
}
JSON データの階層
triggerBody()
triggerBody().eventDataSchemaVersion
triggerBody().eventData.osInfo
triggerBody().eventData.osInfo.version
{
"eventDataSchemaVersion": "1.0",
"message": "Listening on 'http://localhost:4723/'",
"eventData": {
"osInfo": {
"name": "Microsoft Windows NT 6.2.9200.0",
"version": "6.2.9200.0",
"arch": "Win32NT"
},
"machineInfo": {
"screenResolution": "1920x1200",
"nbCores": "8",
"netversion": "4.0.30319.42000"
}
}
}
JSON データの階層
triggerBody()
triggerBody().eventDataSchemaVersion
triggerBody().eventData.osInfo
triggerBody().eventData.osInfo.version
{
"name": "shared_bot",
"id": "/providers/Microsoft.PowerApps/apis/shared_bot",
"type": "Microsoft.PowerApps/apis",
"properties": {
"displayName": "Bot",
"iconUri": "https://connectorassets.blob.core.windows.net/assets/BotFramework.png",
"iconBrandColor": "#007EE5",
"apiEnvironment": "Shared",
"isCustomApi": false,
"runtimeUrls": [
"https://firstrelease-001.azure-apim.net/apim/bot"
],
"primaryRuntimeUrl": "https://firstrelease-001.azure-apim.net/apim/bot",
"capabilities": [
"actions"
],
"description": "Bot service allows sending and receiving messages through the Microsoft Bot Framework.",
"createdTime": "2017-01-12T00:45:42.3934203Z",
"changedTime": "2017-11-01T22:26:13.5446789Z",
"tier": "Standard",
"publisher": "Microsoft"
}
}
triggerBody().properties.capabilities[0]
triggerBody()[‘properties’][‘capabilities’][0]
JSON データの配列
JSON データの参照
{
"eventDataSchemaVersion": "1.0",
"schemaVersion": "1.0",
"correlationId": "bddc4200-ebef-4c70-8430-a4a55bf22f8c",
"clientSessionId": "",
"clientRequestId": "",
"component": "ServerHost",
"eventType": "NotSpecified",
"traceLevel": "Info",
"operationName": "RpaWebServer.Start",
"eventTimestamp": "2019-11-25T01:27:39.3100838Z",
"message": "Listening on 'http://localhost:4723/'",
"eventData": {
"osInfo": {
"name": "Microsoft Windows NT 6.2.9200.0",
"version": "6.2.9200.0",
"arch": "Win32NT"
},
"machineInfo": {
"screenResolution": "1920x1200",
"nbCores": "8",
"netversion": "4.0.30319.42000"
}
}
}
eventDataSchemaVersion の参照
triggerBody()[‘eventDataSchemaVersion’]
triggerBody().eventDataSchemaVersion
キー名を文字列として指定
または . や ? の後に要素名として指定
JSON データの参照
triggerBody()[‘eventDataSchemaVersion’]
triggerBody().eventDataSchemaVersion
書き方はどちらでも問題ない
triggerBody()?[‘eventDataSchemaVersion’]
triggerBody()?eventDataSchemaVersion
? 演算子を使う方が(ワークフローにとって)安全
?演算子
{
"eventDataSchemaVersion": "1.0",
"schemaVersion": "1.0",
"correlationId": "bddc4200-ebef-4c70-8430-a4a55bf22f8c",
"clientSessionId": "",
"clientRequestId": "",
"component": "ServerHost",
"eventType": "NotSpecified",
"traceLevel": "Info",
"operationName": "RpaWebServer.Start",
"eventTimestamp": "2019-11-25T01:27:39.3100838Z",
"message": "Listening on 'http://localhost:4723/'",
"eventData": {
"osInfo": {
"name": "Microsoft Windows NT 6.2.9200.0",
"version": "6.2.9200.0",
"arch": "Win32NT"
},
"machineInfo": {
"screenResolution": "1920x1200",
"nbCores": "8",
"netversion": "4.0.30319.42000"
}
}
}
タイプミスなどで
存在しない値を指定した場合
triggerBody()[‘machineInfo’]
triggerBody(). machineInfo
ワークフローの実行時エラーとなり
後続のアクションへ遷移する
triggerBody()?[‘machineInfo’]
triggerBody()?machineInfo
実行時エラーとはならずに
空文字列(Empty)となる
triggerBody().eventData. machineInfo
が正しいパス
デザイナー上での ? 演算子サポート
ダイアログから値を選択した場合に
自動で?演算子が使われるケースがある
デザイナー側で認識しているスキーマ情報で
Required となるかどうかで記述が変化
必須(Required) の場合は?演算子は利用しない
省略可の場合は?演算子を利用する
デザイナー側で意図に反する補填が行われる場合は
スキーマ情報が意図しない形で認識されている
対象のキー名記載
[]でキー名を文字列で指定する時に
〇〇〇/×××/△△△~
とパスのように記述が可能
通常の記載とはデザイナー上での表記が
異なりパスが見える形になる
Experimental Features
現時点では英語表示に設定した場合に限り
表示が切り替わる
指定することは日本語環境でも利用可能
Value, Type and Actions
{
"key-1": "2019-12-07T00:00:00.000Z",
"key-2": {
"key-2-1": 10,
"key-2-2": "10“
},
"key-3": [
"1", 1, "2", 2
]
}
JSON スキーマ情報ありとなしで挙動が変わるか(日付)
スキーマの有り無しは挙動に影響なく
取得した値をもとに純粋に処理が行われる
JSON スキーマ情報ありとなしで挙動が変わるか(数値)
スキーマの有り無しは挙動に影響なく
取得した値をもとに純粋に処理が行われる
{
"key-1": "2019-12-07T00:00:00.000Z",
"key-2": {
"key-2-1": 10,
"key-2-2": "10“
},
"key-3": [
"1", 1, "2", 2
]
}
{
"key-1": "2019-12-07T00:00:00.000Z",
"key-2": {
"key-2-1": 10,
"key-2-2": "10“
},
"key-3": [
"1", 1, "2", 2
]
}
配列は値で挙動が変わるか(数値)
{
"key-1": "2019-12-07T00:00:00.000Z",
"key-2": {
"key-2-1": 10,
"key-2-2": "10“
},
"key-3": [
"1", 1, "2", 2
]
}
配列は値で挙動が変わるか(文字/数値)
配列の場合も基本ルールは同じ
取得した値をもとに処理が行われる
JSON 情報の更新
addProperty
setProperty
removeProperty
JSON オブジェクトにキーと値を追加
既に存在する場合はエラー
既存のキー名に対応する値を設定
存在しないキー名の場合は新規追加
既存のキー名に対応する値を削除
存在しないキー名の場合はそのまま
処理結果を「新しい JSON オブジェクト」として返却
~Property関数が非常に使いにくい理由
パス形式の指定ができないため、子プロパティの操作が行えない
setProperty(body('JSON_の解析'),'key-2/key-2-1',1000)
と書いた場合の挙動
~Property関数による子プロパティの操作
{
"key-1": "2019-12-07T00:00:00.000Z",
"key-2": {
"key-2-1": 10,
"key-2-2": "10“
},
"key-3": [
"1", 1, "2", 2
]
}
setProperty(body('JSON_の解析'),
'key-2',
setProperty(body('JSON_の解析')?['key-2'],
'key-2-2',
'1000'
)
)
子プロパティを書き換える場合は
親プロパティから階層的に記述が必要
~Property関数による子プロパティの操作
作成2アクションの結果が
新しい JSON 値のオブジェクトに
場面によっては JSON 値を
全て記述したほうが楽
JSONデータを自分で作成する
データ操作コネクタの
「JSONの解析」
「作成」アクションなどで
JSONデータを作成できる
「作成」アクション時は
Json 関数を利用する必要がある
JSONデータのスキーマを自分で作成する
• きれいな形であることを重要視しない
• できるだけ階層構造を利用しない
• データが必要な量、連携できればよい
{
"key-1": "2019-12-07T00:00:00.000Z",
"key-2": {
"key-2-1": 10,
"key-2-2": "10“
},
"key-3": [
"1", 1, "2", 2
]
}
{
"key-1": "2019-12-07T00:00:00.000Z",
"key-2-1": 10,
"key-2-2": "10“,
"key-3": [
"1", 1, "2", 2
]
}
見て理解しやすい形は人によって異なるので
最初は気にしないのがベター
まとめ
• JSON の基本を理解すると書き方や読み方も思いつく
• ~Property 系の関数は残念
• きれいな形にとらわれない
https://aka.ms/logicflowjp-fb
Power Automate/Logic Apps コミュニティ

JSON Value into Power Automate