Azure-docs: Need sample to use "copy" syntax to create array of scalar values (instead of structures)

Created on 14 Dec 2018  Â·  7Comments  Â·  Source: MicrosoftDocs/azure-docs

I need to construct array of integers. All what I may to create is array of structures like
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"hostedServices": {
"type": "array",
"defaultValue": [
{
"name": "name1",
"httpPort": 8995
},
{
"name": "name2",
"httpPort": 8901
}
]
}
},
"variables": {
"hostedServicesHttpPorts": {
"copy": [
{
"name": "hostedServicesHttpPorts",
"count": "[length(parameters('hostedServices'))]",
"input": {
"value": "[parameters('hostedServices')[copyIndex('hostedServicesHttpPorts')].httpPort]"
}
}
]
}
},
"resources": [],
"outputs": {
"hostedServicesHttpPorts": {
"value": "[variables('hostedServicesHttpPorts')]",
"type": "object"
}
}
}

How may I project input array of objects into array of integers? Following syntax is not correct:

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"hostedServices": {
"type": "array",
"defaultValue": [
{
"name": "name1",
"httpPort": 8995
},
{
"name": "name2",
"httpPort": 8901
}
]
}
},
"variables": {
"hostedServicesHttpPorts": {
"copy": [
{
"name": "hostedServicesHttpPorts",
"count": "[length(parameters('hostedServices'))]",
"input": {
"value": "[parameters('hostedServices')[copyIndex('hostedServicesHttpPorts')].httpPort]"
}
}
]
}
},
"resources": [],
"outputs": {
"hostedServicesHttpPorts": {
"value": "[variables('hostedServicesHttpPorts')]",
"type": "object"
}
}
}


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

assigned-to-author azure-resource-managesvc product-issue triaged

All 7 comments

@oleksandr-bilyk - it looks like you are trying to take a property from an array of objects, and create a new array from the values for that property. Is that right? I don't see an easy way to do that. If you know the number of elements in the array, you could use createarray and just hardcode the array index values.

Copy doesn't work because input is an object so you will always get the values wrapped in an object.

Can you pass in the parameter values differently? You could have the http ports in a separate parameter of type array.

@tfitzmac Thank you for reply. Creating additional parameter will cause ports duplications because we need two duplicating structures

  1. list of objects with host, http, https ports for Public IP addresses, Load balancer rules and probes.
  2. Array of ports just for Networks Security group.
    Currently we already using such solution as you described.
    However, please, consider following features:
  3. Implement output not only array of objects but array of any type as well using syntax
    [parameters('hostedServices')[copyIndex('hostedServicesHttpPorts')].httpPort]"
  4. Implement special function that will make object array projection to get array of properties like C# Linq select e.g. [select(inputArray, 'propertyNameToProject').
    It may be very useful feature!

@bmoore-msft - do you know of a way to create an array of integers from one property on an array of objects? Copy doesn't seem to work because it creates an array of objects. createarray works but would require a hardcoded number of elements.

Unfortunately no good way to create a dynamic array in the language today... it's on the backlog.

@oleksandr-bilyk - thanks for this example and feedback. At this point, there is not an update we can make to the documentation, but we will update it when the capabilities change.

please-close

Thank you @tfitzmac and @bmoore-msft

Was this page helpful?
0 / 5 - 0 ratings

Related issues

spottedmahn picture spottedmahn  Â·  3Comments

behnam89 picture behnam89  Â·  3Comments

Agazoth picture Agazoth  Â·  3Comments

bdcoder2 picture bdcoder2  Â·  3Comments

JamesDLD picture JamesDLD  Â·  3Comments