ARM テンプレート用のオブジェクト関数

Resource Manager には、Azure Resource Manager テンプレート (ARM テンプレート) でオブジェクトを操作する関数が複数用意されています。

ヒント

ARM テンプレートと同じ機能を備え、構文も使いやすいため、Bicep をお勧めします。 詳細については、オブジェクト関数に関するページを参照してください。

contains

contains(container, itemToFind)

配列に値が含まれるかどうか、オブジェクトにキーが含まれるかどうか、または文字列に部分文字列が含まれるかどうかを確認します。 文字列比較では大文字・小文字を区別します。 ただし、オブジェクトにキーが含まれているかどうかをテストする場合、比較で大文字・小文字を区別しません。

Bicep では、contains 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
container はい 配列、オブジェクト、文字列 検索対象の値を含む値。
itemToFind はい 文字列または整数 検索対象の値。

戻り値

項目が見つかった場合は True、それ以外の場合は False です。

次の例では、contains をさまざまな型で使用する方法を示します。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "stringToTest": {
      "type": "string",
      "defaultValue": "OneTwoThree"
    },
    "objectToTest": {
      "type": "object",
      "defaultValue": {
        "one": "a",
        "two": "b",
        "three": "c"
      }
    },
    "arrayToTest": {
      "type": "array",
      "defaultValue": [ "one", "two", "three" ]
    }
  },
  "resources": [
  ],
  "outputs": {
    "stringTrue": {
      "type": "bool",
      "value": "[contains(parameters('stringToTest'), 'e')]"
    },
    "stringFalse": {
      "type": "bool",
      "value": "[contains(parameters('stringToTest'), 'z')]"
    },
    "objectTrue": {
      "type": "bool",
      "value": "[contains(parameters('objectToTest'), 'one')]"
    },
    "objectFalse": {
      "type": "bool",
      "value": "[contains(parameters('objectToTest'), 'a')]"
    },
    "arrayTrue": {
      "type": "bool",
      "value": "[contains(parameters('arrayToTest'), 'three')]"
    },
    "arrayFalse": {
      "type": "bool",
      "value": "[contains(parameters('arrayToTest'), 'four')]"
    }
  }
}

既定値を使用した場合の前の例の出力は次のようになります。

名前 Type
stringTrue Bool True
stringFalse Bool False
objectTrue Bool True
objectFalse Bool False
arrayTrue Bool True
arrayFalse Bool False

createObject

createObject(key1, value1, key2, value2, ...)

キーと値からオブジェクトを作成します。

createObject 関数は、Bicep ではサポートされていません。 {} を使用してオブジェクトを構築します。 「オブジェクト」を参照してください。

パラメーター

パラメーター 必須 タイプ [説明]
key1 いいえ string キーの名前です。
value1 いいえ int、boolean、string、object、または array キーの値。
その他のキー いいえ string キーのその他の名前。
その他の値 いいえ int、boolean、string、object、または array キーのその他の値。

この関数では、偶数個のパラメーターを指定する必要があります。 各キーには、一致する値が必要です。

戻り値

各キーと値のペアを持つオブジェクト。

次の例では、さまざまな種類の値からオブジェクトが作成されます。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
  ],
  "outputs": {
    "newObject": {
      "type": "object",
      "value": "[createObject('intProp', 1, 'stringProp', 'abc', 'boolProp', true(), 'arrayProp', createArray('a', 'b', 'c'), 'objectProp', createObject('key1', 'value1'))]"
    }
  }
}

既定値を使用した場合の前の例の出力は、次の値を持っている newObject という名前のオブジェクトです。

{
  "intProp": 1,
  "stringProp": "abc",
  "boolProp": true,
  "arrayProp": ["a", "b", "c"],
  "objectProp": {"key1": "value1"}
}

empty

empty(itemToTest)

配列、オブジェクト、または文字列が空かどうかを判断します。

Bicep で、empty 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
itemToTest はい 配列、オブジェクト、文字列 空かどうかを確認する値。

戻り値

値が空の場合は True を、値が空でない場合は False を返します。

次の例では、配列、オブジェクト、および文字列が空かどうかを確認します。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "testArray": {
      "type": "array",
      "defaultValue": []
    },
    "testObject": {
      "type": "object",
      "defaultValue": {}
    },
    "testString": {
      "type": "string",
      "defaultValue": ""
    }
  },
  "resources": [
  ],
  "outputs": {
    "arrayEmpty": {
      "type": "bool",
      "value": "[empty(parameters('testArray'))]"
    },
    "objectEmpty": {
      "type": "bool",
      "value": "[empty(parameters('testObject'))]"
    },
    "stringEmpty": {
      "type": "bool",
      "value": "[empty(parameters('testString'))]"
    }
  }
}

既定値を使用した場合の前の例の出力は次のようになります。

名前 Type
arrayEmpty Bool True
objectEmpty Bool True
stringEmpty Bool True

intersection

intersection(arg1, arg2, arg3, ...)

パラメーターから共通の要素を持つ 1 つの配列またはオブジェクトを返します。

Bicep で、intersection 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
arg1 はい 配列またはオブジェクト 共通の要素の検索に使用する 1 番目の値。
arg2 はい 配列またはオブジェクト 共通の要素の検索に使用する 2 番目の値。
その他の引数 いいえ 配列またはオブジェクト 共通の要素の検索に使用するその他の値。

戻り値

共通の要素を持つ配列またはオブジェクト。

次の例では、intersection を配列およびオブジェクトと共に使用する方法を示します。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "firstObject": {
      "type": "object",
      "defaultValue": {
        "one": "a",
        "two": "b",
        "three": "c"
      }
    },
    "secondObject": {
      "type": "object",
      "defaultValue": {
        "one": "a",
        "two": "z",
        "three": "c"
      }
    },
    "firstArray": {
      "type": "array",
      "defaultValue": [ "one", "two", "three" ]
    },
    "secondArray": {
      "type": "array",
      "defaultValue": [ "two", "three" ]
    }
  },
  "resources": [
  ],
  "outputs": {
    "objectOutput": {
      "type": "object",
      "value": "[intersection(parameters('firstObject'), parameters('secondObject'))]"
    },
    "arrayOutput": {
      "type": "array",
      "value": "[intersection(parameters('firstArray'), parameters('secondArray'))]"
    }
  }
}

既定値を使用した場合の前の例の出力は次のようになります。

名前 Type
objectOutput Object {"one": "a", "three": "c"}
arrayOutput Array ["two", "three"]

items

items(object)

ディクショナリ オブジェクトを配列に変換します。 配列をオブジェクトに変換する方法については、「toObject」を参照してください。

Bicep で、これらのアイテムを使用します。

パラメーター

パラメーター 必須 タイプ 内容
object はい object 配列に変換するディクショナリ オブジェクト。

戻り値

変換されたディクショナリのオブジェクトの配列。 配列内の各オブジェクトには、ディクショナリのキー値を含む key プロパティがあります。 各オブジェクトには、オブジェクトのプロパティを含む value プロパティもあります。

次の例では、ディクショナリ オブジェクトが配列に変換されます。 配列内のオブジェクトごとに、変更された値を使用して新しいオブジェクトが作成されます。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "copy": [
      {
        "name": "modifiedListOfEntities",
        "count": "[length(items(variables('entities')))]",
        "input": {
          "key": "[items(variables('entities'))[copyIndex('modifiedListOfEntities')].key]",
          "fullName": "[items(variables('entities'))[copyIndex('modifiedListOfEntities')].value.displayName]",
          "itemEnabled": "[items(variables('entities'))[copyIndex('modifiedListOfEntities')].value.enabled]"
        }
      }
    ],
    "entities": {
      "item002": {
        "enabled": false,
        "displayName": "Example item 2",
        "number": 200
      },
      "item001": {
        "enabled": true,
        "displayName": "Example item 1",
        "number": 300
      }
    }
  },
  "resources": [],
  "outputs": {
    "modifiedResult": {
      "type": "array",
      "value": "[variables('modifiedListOfEntities')]"
    }
  }
}

上記の例では、以下が返されます。

"modifiedResult": {
  "type": "Array",
  "value": [
    {
      "fullName": "Example item 1",
      "itemEnabled": true,
      "key": "item001"
    },
    {
      "fullName": "Example item 2",
      "itemEnabled": false,
      "key": "item002"
    }
  ]
}

次の例は、items 関数から返される配列を示しています。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "entities": {
      "item002": {
        "enabled": false,
        "displayName": "Example item 2",
        "number": 200
      },
      "item001": {
        "enabled": true,
        "displayName": "Example item 1",
        "number": 300
      }
    },
    "entitiesArray": "[items(variables('entities'))]"
  },
  "resources": [],
  "outputs": {
    "itemsResult": {
      "type": "array",
      "value": "[variables('entitiesArray')]"
    }
  }
}

この例では、以下が返されます。

"itemsResult": {
  "type": "Array",
  "value": [
    {
      "key": "item001",
      "value": {
        "displayName": "Example item 1",
        "enabled": true,
        "number": 300
      }
    },
    {
      "key": "item002",
      "value": {
        "displayName": "Example item 2",
        "enabled": false,
        "number": 200
      }
    }
  ]
}

JSON では、オブジェクトは 0 個以上のキーと値のペアの順序付けられていないコレクションです。 順序付けは実装によって異なる可能性があります。 たとえば、Bicep items() 関数では、アルファベット順でオブジェクトを並べ替えます。 他の場所では、元の順序を保持できます。 この非決定性のため、デプロイのパラメーターと出力と対話するコードを記述するときは、オブジェクト キーの順序について想定することは避けてください。

json

json(arg1)

有効な JSON 文字列を JSON データ型に変換します。

Bicep で、json 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
arg1 はい string JSON に変換する値。 文字列は、適切に書式設定された JSON 文字列である必要があります。

戻り値

指定された文字列の JSON データ型。null が指定された場合は空のオブジェクト。

解説

JSON オブジェクトにパラメーター値または変数を含める必要がある場合、 format関数を使用し、関数に渡す文字列を作成します。

null() を使用して null 値を取得することもできます。

次の例は、json 関数の使用法を示しています。 空のオブジェクトに null を渡すことができることに注意してください。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "jsonEmptyObject": {
      "type": "string",
      "defaultValue": "null"
    },
    "jsonObject": {
      "type": "string",
      "defaultValue": "{\"a\": \"b\"}"
    },
    "jsonString": {
      "type": "string",
      "defaultValue": "\"test\""
    },
    "jsonBoolean": {
      "type": "string",
      "defaultValue": "true"
    },
    "jsonInt": {
      "type": "string",
      "defaultValue": "3"
    },
    "jsonArray": {
      "type": "string",
      "defaultValue": "[[1,2,3 ]"
    },
    "concatValue": {
      "type": "string",
      "defaultValue": "demo value"
    }
  },
  "resources": [
  ],
  "outputs": {
    "emptyObjectOutput": {
      "type": "bool",
      "value": "[empty(json(parameters('jsonEmptyObject')))]"
    },
    "objectOutput": {
      "type": "object",
      "value": "[json(parameters('jsonObject'))]"
    },
    "stringOutput": {
      "type": "string",
      "value": "[json(parameters('jsonString'))]"
    },
    "booleanOutput": {
      "type": "bool",
      "value": "[json(parameters('jsonBoolean'))]"
    },
    "intOutput": {
      "type": "int",
      "value": "[json(parameters('jsonInt'))]"
    },
    "arrayOutput": {
      "type": "array",
      "value": "[json(parameters('jsonArray'))]"
    },
    "concatObjectOutput": {
      "type": "object",
      "value": "[json(concat('{\"a\": \"', parameters('concatValue'), '\"}'))]"
    }
  }
}

既定値を使用した場合の前の例の出力は次のようになります。

名前 Type
emptyObjectOutput Boolean True
objectOutput Object {"a": "b"}
stringOutput String テスト
booleanOutput Boolean True
intOutput Integer 3
arrayOutput Array [ 1, 2, 3 ]
concatObjectOutput Object { "a": "demo value" }

length

length(arg1)

配列内の要素、文字列内の文字、またはオブジェクト内のルート レベル プロパティの数を返します。

Bicep では、length 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
arg1 はい array、string、または object 要素の数を取得するために使用する配列、文字の数を取得するために使用する文字列、またはルート レベル プロパティの数を取得するために使用するオブジェクト。

戻り値

整数。

次の例では、length を配列および文字列と共に使用する方法を示します。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "arrayToTest": {
      "type": "array",
      "defaultValue": [
        "one",
        "two",
        "three"
      ]
    },
    "stringToTest": {
      "type": "string",
      "defaultValue": "One Two Three"
    },
    "objectToTest": {
      "type": "object",
      "defaultValue": {
        "propA": "one",
        "propB": "two",
        "propC": "three",
        "propD": {
          "propD-1": "sub",
          "propD-2": "sub"
        }
      }
    }
  },
  "resources": [],
  "outputs": {
    "arrayLength": {
      "type": "int",
      "value": "[length(parameters('arrayToTest'))]"
    },
    "stringLength": {
      "type": "int",
      "value": "[length(parameters('stringToTest'))]"
    },
    "objectLength": {
      "type": "int",
      "value": "[length(parameters('objectToTest'))]"
    }
  }
}

既定値を使用した場合の前の例の出力は次のようになります。

名前 Type
arrayLength int 3
stringLength int 13
objectLength int 4

null

null()

null を返します。

null 関数は、Bicep では使用できません。 代わりに、null キーワードを使用します。

パラメーター

null 関数では、パラメーターは受け入れられません。

戻り値

常に null である値。

次の例では、null 関数を使用しています。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [],
  "outputs": {
    "emptyOutput": {
      "type": "bool",
      "value": "[empty(null())]"
    }
  }
}

前の例からの出力は次のようになります。

名前 Type
emptyOutput Bool True

union

union(arg1, arg2, arg3, ...)

パラメーターからすべての要素を持つ 1 つの配列またはオブジェクトを返します。 配列の場合、重複する値は 1 回含められます。 オブジェクトの場合、重複するプロパティ名は 1 回だけ含められまれます。

Bicep で、union 関数を使用します。

パラメーター

パラメーター 必須 タイプ 説明
arg1 はい 配列またはオブジェクト 要素の結合に使用される 1 番目の値。
arg2 はい 配列またはオブジェクト 要素の結合に使用される 2 番目の値。
その他の引数 いいえ 配列またはオブジェクト 要素の結合に使用されるその他の値。

戻り値

配列またはオブジェクト。

注釈

UNION 関数では、パラメーターのシーケンスを使用して、結果の順序と値を決定します。

配列の場合、関数により、最初のパラメーターの各要素が反復処理され、まだ存在していない場合は結果に追加されます。 次に、2 番目のパラメーターと追加のパラメーター (ある場合) に対して処理が繰り返されます。 値が既に存在する場合は、配列内の以前の配置が保持されます。

オブジェクトの場合、最初のパラメーターのプロパティ名と値が結果に追加されます。 以降のパラメーターでは、結果に新しい名前 (ある場合) が追加されます。 後のパラメーターに同じ名前のプロパティがある場合、既存の値はその値で上書きされます。 プロパティの順序は保証されません。

union 関数は、最上位レベルの要素だけでなく、その中の入れ子になった配列とオブジェクトも再帰的にマージします。 次のセクションの 2 番目の例を参照してください。

次の例では、union を配列およびオブジェクトと共に使用する方法を示します。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "firstObject": {
      "type": "object",
      "defaultValue": {
        "one": "a",
        "two": "b",
        "three": "c1"
      }
    },
    "secondObject": {
      "type": "object",
      "defaultValue": {
        "three": "c2",
        "four": "d",
        "five": "e"
      }
    },
    "firstArray": {
      "type": "array",
      "defaultValue": [ "one", "two", "three" ]
    },
    "secondArray": {
      "type": "array",
      "defaultValue": [ "three", "four" ]
    }
  },
  "resources": [
  ],
  "outputs": {
    "objectOutput": {
      "type": "object",
      "value": "[union(parameters('firstObject'), parameters('secondObject'))]"
    },
    "arrayOutput": {
      "type": "array",
      "value": "[union(parameters('firstArray'), parameters('secondArray'))]"
    }
  }
}

既定値を使用した場合の前の例の出力は次のようになります。

名前 Type
objectOutput Object {"one": "a", "two": "b", "three": "c2", "four": "d", "five": "e"}
arrayOutput Array ["one", "two", "three", "four"]

次の例は、ディープ マージ機能を示しています。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "firstObject": {
      "property": {
        "one": "a",
        "two": "b",
        "three": "c1"
      },
      "nestedArray": [
        1,
        2
      ]
    },
    "secondObject": {
      "property": {
        "three": "c2",
        "four": "d",
        "five": "e"
      },
      "nestedArray": [
        3,
        4
      ]
    },
    "firstArray": [
      [
        "one",
        "two"
      ],
      [
        "three"
      ]
    ],
    "secondArray": [
      [
        "three"
      ],
      [
        "four",
        "two"
      ]
    ]
  },
  "resources": [],
  "outputs": {
    "objectOutput": {
      "type": "Object",
      "value": "[union(variables('firstObject'), variables('secondObject'))]"
    },
    "arrayOutput": {
      "type": "Array",
      "value": "[union(variables('firstArray'), variables('secondArray'))]"
    }
  }
}

前の例からの出力は次のようになります。

名前 Type
objectOutput Object {"property":{"one":"a","two":"b","three":"c2","four":"d","five":"e"},"nestedArray":[3,4]}
arrayOutput Array [["one","two"],["three"],["four","two"]]

入れ子になった配列がマージされた場合、objectOutput.nestedArray の値は [1, 2, 3, 4] になり、arrayOutput の値は [["one", "two", "three"], ["three", "four", "two"]] になります。

次のステップ