Convert object to Media or BlobMedia

I have a server function which returns a list of dicts to the client which is currently faulting if the return object is too large. The dict contains BlobMedia and strings.
From the post below, I understand that a Media object as a return is able to be larger than 4Mb but I don’t understand how to convert this list of dicts to a media object. I have tried the following:

but receive a TypeError: Object of type BlobMedia is not JSON serializable

If I remove the BlobMedia object I get the following error when the json.loads() function runs in the client:

AttributeError: 'list' object has no attribute 'get_bytes'

Any suggestions please as my serialisation knowledge is not strong.

Hello @rickhurlbatt
on what statement you get the TypeError: Object of type BlobMedia is not JSON serializable error?
And the second error, is it the same statement or a different one?
Can you share a clone app?

BR

The first error comes from the statement

return anvil.BlobMedia("application/json", json.dumps(data))

when I have a BlobMedia in the dict.

If I remove the BlobMedia image, I was getting that second error statement from the client side after the return on the data = json.loads(returned_media.get_bytes()) but I can no longer replicate that.

The current error is

TypeError: BlobMedia content must be a byte string. on the statement
return anvil.BlobMedia("application/json", json.dumps(data))

I cannot for the life of me get a the BlobMedia to accept either a json.dumps as it requires a byte string. What other options are there?
Is there a better way to return a large object composed of images and text from the server to the client?

Hi @rickhurlbatt
I find it difficult to follow you without a clone-app example to look into.
However, reading what you’ve written, I’d suggest to try to convert explicitly your BlobMedia image content to a BASE64 string in your list of dicts, then convert it back to binary in the client.
This way your json.dumps(data) serialisation should work and - in the end - you are just doing explicitly what you were implicitly expecting it to do: convert a binary object to somw sort of string.

Thanks @aldo.ercolani this is exactly what I needed to do.
When I get a chance I will try post a clone link to a pared down version.

1 Like