LoginSignup
4
2

More than 3 years have passed since last update.

Azure Logic Apps の JSON 解析で nullable な型を定義する

Last updated at Posted at 2020-07-15

Azure Logic Apps で JSON を扱う時に、こういう JSON があったとします。

{
  "value": [
    {
      "foo": "bar"
    },
    {
      "foo": null
    }
  ]
}

これを Logic Apps の JSON 解析の サンプルのペイロードを使用してスキーマを生成する という機能を使って型定義を生成すると、以下のようになります。

{
    "type": "object",
    "properties": {
        "value": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "foo": {
                        "type": "string"
                    }
                },
                "required": [
                    "foo"
                ]
            }
        }
    }
}

foo という key が必須の string 型の配列として判定されてしまいました。
これでは実際に JSON 解析を行った時に null な値が来ると、パースエラーになってしまいます。困りましたね。

type には型を表現する文字列だけではなく、配列も渡すことが出来るので、以下のように ["string", "null"] として定義を修正してあげましょう

{
    "type": "object",
    "properties": {
        "value": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "foo": {
                        "type": ["string", "null"]
                    }
                },
                "required": [
                    "foo"
                ]
            }
        }
    }
}

これで nullable な値を適切に JSON 解析することが出来ます。やりましたね!

4
2
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
4
2