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

。_饼干妹妹 提交于 2019-12-11 16:12:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!