How to serialize Inventory to JSON?

59 views
Skip to first unread message

Alen Šiljak

unread,
May 25, 2025, 7:05:40 PM5/25/25
to Beancount
Hi! I have a simple API that runs a bean-query
```
select account, convert(value(sum(position)), 'EUR') as value
```
to get all current balances in the same currency. Then the result is serialized by FastAPI. Or at least it tries to.
Unfortunately, it fails to serialize Inventories with 
```
TypeError: unhashable type: 'list'
```
Is there a way to get something serializable - a simple string, or a number/string combination for the amounts - from the output of convert() function?

Martin Blais

unread,
May 25, 2025, 9:38:26 PM5/25/25
to bean...@googlegroups.com
I'm not sure where FastAPI comes into this story.
If you're querying using Python code you're getting Python objects which, with a little bit of data wrangling you should be able to convert to JSON.


--
You received this message because you are subscribed to the Google Groups "Beancount" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beancount+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/beancount/7844ca48-d018-4967-90ea-8016d9bf5c49n%40googlegroups.com.

Alen Šiljak

unread,
May 26, 2025, 2:36:18 AM5/26/25
to Beancount

FastAPI is just a server. It automatically converts the output to JSON. The queries are sent from the client. Technically, it could iterate over the result and replace Inventories with a tuple or something like that. Even just a string would work because it gets parsed back into objects inside Cashier.
Was just wondering if there is a better way or if something could be added to make the serialization easier.

Alen Šiljak

unread,
May 26, 2025, 5:29:27 AM5/26/25
to Beancount
The issue in the server component is that it does not know what the result is, since the queries are coming from the client and just being forwarded to bean-query.
Anyway, I'm now iterating over the results and converting sets (Inventories) into lists, which seems to do the job for now.
Thank you for the tip.
Reply all
Reply to author
Forward
0 new messages