Reputation: 4405
I'm new to Python; using Jupyter nb here.
When setting a variable to the results of an az cli
command, I get a json array but one riddled with single quotes.
When I run the command alone, without setting it to a variable, the response is clean json.
How do I get the variable result as clean json?
Example1: (This was fixed by using .join
as explained in the comments)
new_mi = !az identity create --name $mi_name --resource-group $rg_name --location $rc_location
new_mi
['{',
' "clientId": "redacto",',
' "clientSecretUrl": "https://control-westus2.identity.azure.net/subscriptions/redacto/resourcegroups/rg_azureFunction/providers/Microsoft.ManagedIdentity/userAssignedIdentities/mi_azureFunction/credentials?tid=redacto&oid=redacto&aid=redacto",',
' "id": "/subscriptions/redacto/resourcegroups/rg_azureFunction/providers/Microsoft.ManagedIdentity/userAssignedIdentities/mi_azureFunction",',
' "location": "westus2",',
' "name": "mi_azureFunction",',
' "principalId": "redacto",',
' "resourceGroup": "rg_azureFunction",',
' "tags": {',
' "lang": "python",',
' "owner": "redacto",',
' "project": "test"',
' },',
' "tenantId": "redacto",',
' "type": "Microsoft.ManagedIdentity/userAssignedIdentities"',
'}']
Example2: Different az cli
command, but .join
does not work here
.join
is valid json due to the message at the beginning. .join
?sa_new = !az storage account create --name $sa_name --resource-group $rg_name --access-tier Cool --default-action Deny --kind StorageV2 --sku Standard_LRS --subscription $sub_id --tags $sa_tags
sa_new
["WARNING: The default kind for created storage account will change to 'StorageV2' from 'Storage' in the future",
'{',
' "accessTier": "Cool",',
' "azureFilesIdentityBasedAuthentication": null,',
' "creationTime": "2020-05-21T05:28:13.115847+00:00",',
' "customDomain": null,',
' "enableHttpsTrafficOnly": true,',
' "encryption": {',
' "keySource": "Microsoft.Storage",',
' "keyVaultProperties": null,',
' "services": {',
' "blob": {',
' "enabled": true,',
' "lastEnabledTime": "2020-05-21T05:28:13.193965+00:00"',
' },',
' "file": {',
' "enabled": true,',
' "lastEnabledTime": "2020-05-21T05:28:13.193965+00:00"',
' },',
' "queue": null,',
' "table": null',
' }',
' },',
' "failoverInProgress": null,',
' "geoReplicationStats": null,',
' "id": "/subscriptions/redacto/resourceGroups/redacto/providers/Microsoft.Storage/storageAccounts/redacto",',
' "identity": null,',
' "isHnsEnabled": null,',
' "kind": "StorageV2",',
' "largeFileSharesState": null,',
' "lastGeoFailoverTime": null,',
' "location": "westus2",',
' "name": "redacto",',
' "networkRuleSet": {',
' "bypass": "AzureServices",',
' "defaultAction": "Deny",',
' "ipRules": [],',
' "virtualNetworkRules": []',
' },',
' "primaryEndpoints": {',
' "blob": "https://redacto.blob.core.windows.net/",',
' "dfs": "https://redacto.dfs.core.windows.net/",',
' "file": "https://redacto.file.core.windows.net/",',
' "queue": "https://redacto.queue.core.windows.net/",',
' "table": "https://redacto.table.core.windows.net/",',
' "web": "https://redacto.z5.web.core.windows.net/"',
' },',
' "primaryLocation": "westus2",',
' "provisioningState": "Succeeded",',
' "resourceGroup": "redacto",',
' "secondaryEndpoints": null,',
' "secondaryLocation": null,',
' "sku": {',
' "capabilities": null,',
' "kind": null,',
' "locations": null,',
' "name": "Standard_LRS",',
' "resourceType": null,',
' "restrictions": null,',
' "tier": "Standard"',
' },',
' "statusOfPrimary": "available",',
' "statusOfSecondary": null,',
' "tags": {',
' "owner": "redacto",',
' "project": "test"',
' },',
' "type": "Microsoft.Storage/storageAccounts"',
'}']
sa_new = json.loads("\n".join(sa_new))
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Example3: This command has the same issue, a "WARNING" message at the top makes the response invalid JSON
az_new = !az functionapp create --consumption-plan-location westus --name $af_name --os-type Linux --resource-group $rg_name --runtime python --storage-account $sa_name --disable-app-insights true
az_new
["WARNING: Your Linux function app 'redacto', that uses a consumption plan has been successfullycreated but is not active until content is published usingAzure Portal or the Functions Core Tools.",
'{',
' "availabilityState": "Normal",',
' "clientAffinityEnabled": false,',...
Upvotes: 1
Views: 147
Reputation: 2360
When you use !
to run command in Jupyter, it returns the output as a list of rows. A more convenient way might be to use os.popen
or subprocess.run
or similar command, and then parse the json.
import os
import json
json.loads(os.popen("az identity create --name $mi_name --resource-group $rg_name --location $rc_location
new_mi").read())
Upvotes: 1