Some questions about serializing data with vSphere Automation SDK for Python


I’m actually working on developing and API for managing cloud Infrastructures by using Django and [this](https://github.com/vmware/vsphere-automation-sdk-python) SDK. I have no problem connecting to my vCenter and retrieving data, but I’m not capable of serializing the objects received by the methods and convert them to JSON format:

vsphere_client = connections.vCenterConnection(vcenter)
var = vsphere_client.vcenter.VM.list()
return Response(json.dumps(var))

In this example, the json function returns me an error like this:

`Object of type “Summary” is not serializable`

The format of the variable is (according to the documentation provided):

`[Summary(vm=’vm-58′, name=’standalone-20e4bd3af-esx.0-vm.0′, power_state=State(string=’POWERED_OFF’), cpu_count=1, memory_size_mib=256), …]`

Since the SDK documentation does not include information related to this topic, I would like some help about how to manage this type of objects.


**EDIT**: After long searching, I found a module inside the SDK to manipulate data. Here I include a simple example of how it works if any of you has the same trouble:

class vmList(APIView):
def get(self, request, vcenter, format=None):
list = vsphere_client.vcenter.VM.list()
data = [] for element in list:
vm = vsphere_client.vcenter.VM.get(element.vm)
return Response(data)

View Reddit by Highly-SedatedView Source

Related Articles

One Comment

  1. Have you tried fully casting the object? Its possible the .list() method still returns an internal data type, Summary.

    `var = vsphere_client.vcenter.VM.list()`

    `return Response(json.dumps(list(var)))`


    Alternatively you could try a list comprehension and cast it using the iterable

    `return Response(json.dumps([x for x in var]))`


    All of that being said, the format of the Summary object you provided doesn’t look like valid JSON, you may have better luck with a .dict() method if there is one. Or you may need to create your own return Response, which translates Summary object to json()

Leave a Reply

Your email address will not be published. Required fields are marked *