Convert data type response to dict

When I execute a query lambda with python

results = qlambda.execute(parameters=params)
print(type(results))

this shown me

<class 'rockset.swagger_client.models.query_response.QueryResponse'>

How can I convert this data in dict?
I usually use this code

if 'results' in results:  #to check ik results key is in the dict

The results of this code is

    if 'results' in results:
  File "/usr/lib/python3.10/site-packages/rockset/swagger_client/models/query_response.py", line 394, in __getitem__
    return getattr(self, item)
TypeError: getattr(): attribute name must be string

When i try to convert to dict wthis this code

resultados = dict(results)

this is the same error

Traceback (most recent call last):
  File "/tmp/novale.py", line 16, in <module>
    resultados = dict(results)
  File "/usr/lib/python3.10/site-packages/rockset/swagger_client/models/query_response.py", line 394, in __getitem__
    return getattr(self, item)
TypeError: getattr(): attribute name must be string

When i use this web site https://jsonformatter.curiousconcept.com/ to see the data structure od the response i have some errors like that

* **Info:** Replaced incorrect quotes.
* **Info:** Inserted missing quotes.
* **Error:** Expecting comma, colon or }, not string. *[Code 11, Structure 192]*
* **Error:** Expecting comma, colon or }, not string. *[Code 11, Structure 197]*
* **Error:** Expecting comma, colon or }, not string. *[Code 11, Structure 1000]*
* **Error:** Expecting comma, colon or }, not string. *[Code 11, Structure 1005]*
* **Error:** Expecting comma, colon or }, not string. *[Code 11, Structure 1840]*
* **Error:** Expecting comma, colon or }, not string. *[Code 11, Structure 1845]*
* **Error:** Expecting comma, colon or }, not string. *[Code 11, Structure 2684]*
* **Error:** Expecting comma, colon or }, not string. *[Code 11, Structure 2689]*

maybe the problem is the data and I can not convert the data in standar python dict.

1 Like

When you print(dir(results)) what do you see?

Just dug in a little further. When you dir you can do something like this: print(type(results.results)) which gets you a list type.

Something like this: print(results.results) looks like this:

[{'?field0': 2}]

You can also do this as well print(results.to_dict) which returns something like this:

{'collections': [],
 'column_fields': [{'name': '?field0', 'type': ''}],
 'query_errors': None,
 'query_id': '701e1e7b-c95f-4726-951e-8b4c01421a68:3aUfb1b:0',
 'results': [{'?field0': 7}],
 'stats': {'elapsed_time_ms': 2, 'throttled_time_micros': 0},
 'warnings': None}

If you got either of those returned would this be ok for you and what you want to do? If not, what would be an ideal thing you’re looking for? Please let me know if this helped solved your questions.

Thanks!
n

Ok, when I execute

results = qlambda.execute(parameters=params)
#print(results)
datos = results.to_dict
print(datos)
for e in datos['results']:
    print(e)
Traceback (most recent call last):
  File "/home/jrios/ego/enrique3.py", line 18, in <module>
    for e in datos['results']:
TypeError: 'method' object is not subscriptable

I execute

results = qlambda.execute(parameters=params)
#print(results)
datos = results.to_dict
print(datos)
for e in datos['results']:
    print(e)

The error is:

    for e in datos['results']:
TypeError: 'method' object is not subscriptable

There are 3 options I think may help you with what you need:

  1. Use to_dict() to iterate over the items:
results = qlambda.execute(parameters=params)
#print(results)
datos = results.to_dict()
print(datos)
for k, v in datos.items():
    print(k,v)
  1. Get a list and iterate over it:
results = qlambda.execute(parameters=params)
#print(results)
datos = results.results
print(datos)
for item in datos:
    print(item)
  1. You can access the property __dict__:
results = qlambda.execute()
for (k, v) in results.__dict__.items():
    print(k,v)

does this help you for what you need? If not, please let me know how I can help further.

@jrios - catching up! does this help ?