简体   繁体   中英

Get Virtual Machine sizes list in json format using azure-sdk-for-python

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM