问题
I'm trying to use azure-sdk-for-python library to connect to azure cloud and to execute certain operations.
I followed the below code samples provided in Azure docs to start with the API that get the virtual machine sizes based on a location.
For getting Authentication client: https://docs.microsoft.com/en-us/python/azure/python-sdk-azure-authenticate?view=azure-python
Once the ComputeManagementclient Object is obtained, The following lines retrieves list of VirtualMachineSize objects.
client = CompteManagementClient(credentials, subscription_id)
vmSizesList = client.virtual_machine_sizes.list()
I want this output to be in json format instead of object. So I tried the below statement: result = json.dumps(vmSizesList)
This results in error "object is not serializable".
The other way I think is only to loop and to manually build json structured data. Any help would be grateful.
Please comment in case you need any additional info.
回答1:
Result of a list
call return an iterable, so first you would have to consume this iterable as a list. Then, each object will contain a serialize
method that will put back the object into its JSON form.
In practical terms:
client = CompteManagementClient(credentials, subscription_id)
vmSizesList = [vm_size.serialize() for vm_size in client.virtual_machine_sizes.list()]
json.dumps(vmSizesList)
来源:https://stackoverflow.com/questions/52061293/get-virtual-machine-sizes-list-in-json-format-using-azure-sdk-for-python