LoginSignup
2
1

Power AppsからPower Automateへ値を渡すときに活躍する「JSONの解析」

Last updated at Posted at 2024-05-09

はじめに

Power AppsPower Automateの連携、
非常に強力な機能です。

豊富なコネクタから、テクニカルな自動化まで一気通貫で担うことができます。

しかし、Power AppsからPower Automateフローをトリガーするに当たり、
引数の設定が若干手間だったりします。

Power Apps
FlowtriggeredbyaPowerapp.Run(
   EditForm3.LastSubmit.Email,
   TextInput1.Text
);

引用元 Microsoft learn

引数をPower Automate側で、あらかじめ設定しておく必要があります。

トリガー PowerApps (V2)を覗いてみる

Power AppsからPower Automateに、データを渡すときは、PowerApps (V2) トリガーから、変数の型を指定して取得します。

{
  "type": "Request",
  "kind": "PowerAppV2",
  "inputs": {
    "schema": {
      "type": "object",
      "properties": {
        "text": {
          "description": "入力を指定してください",
          "title": "入力",
          "type": "string",
          "x-ms-content-hint": "TEXT",
          "x-ms-dynamically-added": true
        },
        "boolean": {
          "description": "[はい] または [いいえ] を選択してください",
          "title": "はい/いいえ",
          "type": "boolean",
          "x-ms-content-hint": "BOOLEAN",
          "x-ms-dynamically-added": true
        },
        "file": {
          "description": "ファイルまたはイメージを選択してください",
          "title": "ファイルのコンテンツ",
          "type": "object",
          "properties": {
            "contentBytes": {
              "type": "string",
              "format": "byte"
            },
            "name": {
              "type": "string"
            }
          },
          "x-ms-content-hint": "FILE",
          "x-ms-dynamically-added": true
        },
        "email": {
          "description": "メール アドレスを入力してください。",
          "format": "email",
          "title": "電子メール",
          "type": "string",
          "x-ms-content-hint": "EMAIL",
          "x-ms-dynamically-added": true
        },
        "number": {
          "description": "数を入力してください",
          "title": "数",
          "type": "number",
          "x-ms-content-hint": "NUMBER",
          "x-ms-dynamically-added": true
        },
        "date": {
          "description": "日付を入力または選択してください (YYYY-MM-DD)",
          "format": "date",
          "title": "トリガーの日付",
          "type": "string",
          "x-ms-content-hint": "DATE",
          "x-ms-dynamically-added": true
        }
      },
      "required": [
        "text",
        "boolean",
        "file",
        "email",
        "number",
        "date"
      ]
    }
  }
}

Power Automateの変数の型に合わせて、Power Appsから値を受け取ることができます。

日本語表記 データ型
テキスト text
はい/いいえ boolean
ファイル file
電子メール email
数値 number
日付 date

image.png

値を呼び出す方法

triggerBody関数で値を受け取ります。

若干曲者ですが、triggerBody関数の引数は、PowerApps (V2)の中で暗黙的に決まります。
「引数はこれで正しいのかな🧐」と怖くなる時もあります。

パラメーターで名前を工夫しても・・・

image.png

引数は

  • text
  • number
  • text_1

と自動的に決まってしまいます。

{
    **引数!** "text": {
      "description": "入力を指定してください",
      "title": "入力",
      "type": "string",
      "x-ms-content-hint": "TEXT",
      "x-ms-dynamically-added": true
    },
    **引数!** "number": {
      "description": "数を入力してください",
      "title": "数",
      "type": "number",
      "x-ms-content-hint": "NUMBER",
      "x-ms-dynamically-added": true
    },
    **引数!** "text_1": {
      "description": "入力を指定してください",
      "title": "入力 1",
      "type": "string",
      "x-ms-content-hint": "TEXT",
      "x-ms-dynamically-added": true
    },
    **引数!** "text_2": {
      "description": "入力を指定してください",
      "title": "Text",
      "type": "string",
      "x-ms-content-hint": "TEXT",
      "x-ms-dynamically-added": true
    },
    **引数!** "text_3": {
      "description": "入力を指定してください",
      "title": "Str",
      "type": "string",
      "x-ms-content-hint": "TEXT",
      "x-ms-dynamically-added": true
    },
    **引数!** "number_1": {
      "description": "数を入力してください",
      "title": "Integer",
      "type": "number",
      "x-ms-content-hint": "NUMBER",
      "x-ms-dynamically-added": true
    },
    **引数!** "boolean": {
      "description": "[はい] または [いいえ] を選択してください",
      "title": "はい/いいえ",
      "type": "boolean",
      "x-ms-content-hint": "BOOLEAN",
      "x-ms-dynamically-added": true
    },
    **引数!** "boolean_1": {
      "description": "[はい] または [いいえ] を選択してください",
      "title": "はい/いいえ 1",
      "type": "boolean",
      "x-ms-content-hint": "BOOLEAN",
      "x-ms-dynamically-added": true
    }
}

画面で多くの引数を設定するのも、なんとなく手間を感じます。

JSON の解析 を使う - シンプルな文字列

こんなときに私は、Power Automateのデータ操作におけるJSON の解析を使用します。
JSON の解析を使うことによって、
ひとつの文字列から、オブジェクトとして複数の値を受け取ることができるからです。

試しに、Power AppsからPower Automateオブジェクト文字列を送信します。

Power Automateに単純にコンテキスト変数でObjを渡す
UpdateContext(
    {
        Obj : {
            ObjectText: "Text",
            ObjectString: "Power Apps",
            Number: 365,
            Boolean: true,
            Date: Today()
        }
    }
);

ParseJSON.Run(JSON(Obj));

Power Automate側は、文字列としてJSON関数の結果を受け取ります。

image.png

JSON の解析用に、{}だけ指定すると、オブジェクトの解析の許容範囲が広がるので、実験に最適だったりします。

image.png

受け取る値はコチラ。

Power Appsの出力
{"Boolean":true,"Date":"2024-05-09","Number":365,"ObjectString":"Power Apps","ObjectText":"Text"}

こちらを、JSON の解析からサンプルのペイロードを使用してスキーマを生成する

Shema
{
    "type": "object",
    "properties": {
        "Boolean": {
            "type": "boolean"
        },
        "Date": {
            "type": "string"
        },
        "Number": {
            "type": "integer"
        },
        "ObjectString": {
            "type": "string"
        },
        "ObjectText": {
            "type": "string"
        }
    }
}

サンプルに合わせて、データ型を定義し、

image.png

body('JSON_の解析')の結果
{
    "Boolean": true,
    "Date": "2024-05-09",
    "Number": 365,
    "ObjectString": "Power Apps",
    "ObjectText": "Text"
}

後続の処理に使うことができます。
文字列から値を評価するからこそ、なせるテクニックです。

Power Appsの式エディタのほうが、コードエディタに近しい形で、変数を定義できるため、
Power Appsからまとめて値を送ってしまいたいな、というときに便利です。

値を厳密に評価するため、データ型が異なると、エラーになる原因にもなります。
※予定していた値が、Nullの場合

UpdateContext(
    {
        Obj : {
            ObjectText: "Text",
            ObjectString: Blank(),
            Number: 365,
            Boolean: true,
            Date: Today()
        }
    }
);

ParseJSON.Run(JSON(Obj));

image.png

エラーにつながるため、沼にハマりやすいです。

配列(Array)を渡すこともできる

JSON関数で、JSONFormat.FlattenValueTablesを設定し、値配列を渡すと、

Arrayを渡すパターン
ParseJSON.Run(JSON(["a", "b", "cde"],JSONFormat.FlattenValueTables));

Power Appsで、扱いづらかった配列が渡せます。

image.png

配列にオブジェクトを格納して渡す

私の記事の中で、かなり多く反響をいただいた記事にも使用しましたが
Table関数JSONに変換して渡すことで、
配列に格納されたオブジェクトとして評価され、動的な数の配列として使用することもできます。

Power Appsの式
ParseJSON.Run(JSON(Table({Item:"Violin123",Location:"France",Owner:"Fabrikam"})))

image.png

オブジェクト変数の中に、Table関数、値配列の場合

- オブジェクトの中にTable関数

オブジェクトの中に、Table関数値配列を含めて、解析を試みた場合、
オブジェクトの中の配列として、活用ができます。

Power Appsの式
UpdateContext(
    {
        Obj : {
            ObjectText: "Text",
            TableArray: Table({Item:"Violin123",Location:"France",Owner:"Fabrikam"},{Item:"Violin456",Location:"Chile"}),
            Number: 365,
            Boolean: true,
            Date: Today()
        }
    }
);

ParseJSON.Run(JSON(Obj));

解析を通じて、データ型が狙い通りに評価されました。

image.png

Schema
{
  "type": "object",
  "properties": {
    "Boolean": {
      "type": "boolean"
    },
    "Date": {
      "type": "string"
    },
    "Number": {
      "type": "integer"
    },
    "ObjectText": {
      "type": "string"
    },
    "TableArray": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "Item": {
            "type": "string"
          },
          "Location": {
            "type": "string"
          },
          "Owner": {
            "type": "string"
          }
        },
        "required": [
          "Item",
          "Location"
        ]
      }
    }
  }
}

- オブジェクトの中に値配列

値配列[]を内包してみると、どうなるでしょうか。

Power Appsの式
UpdateContext(
    {
        Obj : {
            ObjectText: "Text",
            TableArray: [1,2,3],
            Number: 365,
            Boolean: true,
            Date: Today()
        }
    }
);

ParseJSON.Run(JSON(Obj));

上記の場合、配列がkey:valueの形式になります。

Schema
{
    "type": "object",
    "properties": {
        "Boolean": {
            "type": "boolean"
        },
        "Date": {
            "type": "string"
        },
        "Number": {
            "type": "integer"
        },
        "ObjectText": {
            "type": "string"
        },
        "TableArray": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "Value": {
                        "type": "integer"
                    }
                },
                "required": [
                    "Value"
                ]
            }
        }
    }
}

JSON([1, 2, 3],JSONFormat.FlattenValueTables)を使ってみると、

Power Appsの式
UpdateContext(
    {
        Obj : {
            ObjectText: "Text",
            TableArray: JSON([1, 2, 3],JSONFormat.FlattenValueTables),
            Number: 365,
            Boolean: true,
            Date: Today()
        }
    }
);

ParseJSON.Run(JSON(Obj));

TableArray文字列(string)として評価されてしまいました・・・。

image.png

Schema
{
    "type": "object",
    "properties": {
        "Boolean": {
            "type": "boolean"
        },
        "Date": {
            "type": "string"
        },
        "Number": {
            "type": "integer"
        },
        "ObjectText": {
            "type": "string"
        },
        "TableArray": {
            "type": "string"
        }
    }
}

なかなか奥が深い。

おわりに

なんとなくやりづらかった操作も活路が見えますが、
このJSONの解析を使うことによって、発生するエラーもあります。

活用される場合は、エラーと向き合う覚悟をもちつつ、時と場合を考えて使ってみましょう!

記事のいいね❤や拡散は、著者が非常に喜ぶのでジャンジャンしてくださいね♪

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1