1

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://learn.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 Answer 1

4

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.