JSON error when making query using python3

I’m testing my scripts with python3 for the first time and one issue I have encountered is that previously I could make a query asking for all the contents of all fields in the schema of a particular entity and I would receive them in a dict.

Now I get an error saying “TypeError: Object of type dict_keys is not JSON serializable”

Usually I would do a query to get a list of the schema for the entity and then pass that list in the fields parameter when I’m doing a find.

For example…
fields=sg.schema_field_read(entity).keys()
results=sg.find(entity,filters=[(‘project.Project.id’, ‘is’, project_id)],fields=fields)

In python 3 dict keys are a distinct object. You can convert them to a list by wrapping in list: list(sg.schema_field_read(entity).keys()), which is json serializable.

The reason many constructs like map and filter are also their own objects now, is laziness - they return a lazy iterator, which can save on memory usage e.g. if you just need to iterate the value. In the given case, you need the whole value, so you evaluate it eagerly to turn it into a list.

1 Like