|
| 1 | +--- |
| 2 | +title: Linter rule - simplify JSON null |
| 3 | +description: Linter rule - simplify JSON null |
| 4 | +ms.topic: conceptual |
| 5 | +ms.date: 03/20/2023 |
| 6 | +--- |
| 7 | + |
| 8 | +# Linter rule - simplify JSON null |
| 9 | + |
| 10 | +This rule finds `json('null')`. |
| 11 | + |
| 12 | +## Linter rule code |
| 13 | + |
| 14 | +Use the following value in the [Bicep configuration file](bicep-config-linter.md) to customize rule settings: |
| 15 | + |
| 16 | +`simplify-json-null` |
| 17 | + |
| 18 | +## Solution |
| 19 | + |
| 20 | +The following example fails this test because `json('null')` is used: |
| 21 | + |
| 22 | +```bicep |
| 23 | +@description('The name of the API Management service instance') |
| 24 | +param apiManagementServiceName string = 'apiservice${uniqueString(resourceGroup().id)}' |
| 25 | +
|
| 26 | +@description('The email address of the owner of the service') |
| 27 | +@minLength(1) |
| 28 | +param publisherEmail string |
| 29 | +
|
| 30 | +@description('The name of the owner of the service') |
| 31 | +@minLength(1) |
| 32 | +param publisherName string |
| 33 | +
|
| 34 | +@description('The pricing tier of this API Management service') |
| 35 | +@allowed([ |
| 36 | + 'Premium' |
| 37 | +]) |
| 38 | +param sku string = 'Premium' |
| 39 | +
|
| 40 | +@description('The instance size of this API Management service.') |
| 41 | +param skuCount int = 3 |
| 42 | +
|
| 43 | +@description('Location for all resources.') |
| 44 | +param location string = resourceGroup().location |
| 45 | +
|
| 46 | +@description('Zone numbers e.g. 1,2,3.') |
| 47 | +param availabilityZones array = [ |
| 48 | + '1' |
| 49 | + '2' |
| 50 | + '3' |
| 51 | +] |
| 52 | +
|
| 53 | +resource apiManagementService 'Microsoft.ApiManagement/service@2022-08-01' = { |
| 54 | + name: apiManagementServiceName |
| 55 | + location: location |
| 56 | + zones: ((length(availabilityZones) == 0) ? json('null') : availabilityZones) |
| 57 | + sku: { |
| 58 | + name: sku |
| 59 | + capacity: skuCount |
| 60 | + } |
| 61 | + identity: { |
| 62 | + type: 'SystemAssigned' |
| 63 | + } |
| 64 | + properties: { |
| 65 | + publisherEmail: publisherEmail |
| 66 | + publisherName: publisherName |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +You can simplify the syntax by replacing `json('null')` by `null`: |
| 72 | + |
| 73 | +```bicep |
| 74 | +@description('The name of the API Management service instance') |
| 75 | +param apiManagementServiceName string = 'apiservice${uniqueString(resourceGroup().id)}' |
| 76 | +
|
| 77 | +@description('The email address of the owner of the service') |
| 78 | +@minLength(1) |
| 79 | +param publisherEmail string |
| 80 | +
|
| 81 | +@description('The name of the owner of the service') |
| 82 | +@minLength(1) |
| 83 | +param publisherName string |
| 84 | +
|
| 85 | +@description('The pricing tier of this API Management service') |
| 86 | +@allowed([ |
| 87 | + 'Premium' |
| 88 | +]) |
| 89 | +param sku string = 'Premium' |
| 90 | +
|
| 91 | +@description('The instance size of this API Management service.') |
| 92 | +param skuCount int = 3 |
| 93 | +
|
| 94 | +@description('Location for all resources.') |
| 95 | +param location string = resourceGroup().location |
| 96 | +
|
| 97 | +@description('Zone numbers e.g. 1,2,3.') |
| 98 | +param availabilityZones array = [ |
| 99 | + '1' |
| 100 | + '2' |
| 101 | + '3' |
| 102 | +] |
| 103 | +
|
| 104 | +resource apiManagementService 'Microsoft.ApiManagement/service@2022-08-01' = { |
| 105 | + name: apiManagementServiceName |
| 106 | + location: location |
| 107 | + zones: ((length(availabilityZones) == 0) ? null : availabilityZones) |
| 108 | + sku: { |
| 109 | + name: sku |
| 110 | + capacity: skuCount |
| 111 | + } |
| 112 | + identity: { |
| 113 | + type: 'SystemAssigned' |
| 114 | + } |
| 115 | + properties: { |
| 116 | + publisherEmail: publisherEmail |
| 117 | + publisherName: publisherName |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +You can simplify the syntax by selecting **Quick Fix** as shown on the following screenshot: |
| 123 | + |
| 124 | +:::image type="content" source="./media/linter-rule-simplify-json-null/bicep-linter-rule-simplify-json-null-quick-fix.png" alt-text="Screenshot of simplify JSON null quick fix."::: |
| 125 | + |
| 126 | +## Next steps |
| 127 | + |
| 128 | +For more information about the linter, see [Use Bicep linter](./linter.md). |
0 commit comments