Serializing a list of anvil.tables.Row objects

I have a list of anvil.tables.Rows that I want to serialize into a list of dictionaries so that it looks like the following:

[
      { Id: "key-1", Status: "Open", Summary: "Analyze the new requirements gathered from the customer.", Type: "Story", Priority: "Low", Tags: "Analyze,Customer", Estimate: 3.5, Assignee: "Andrew Fuller", RankId: 1 },
      { Id: 2, Status: "InProgress", Summary: "Improve application performance", Type: "Improvement", Priority: "Normal", Tags: "Improvement", Estimate: 6, Assignee: "Andrew Fuller", RankId: 2 }
]

What’s the best way to do just that with the anvil.tables.Row object? I’ve tried accessing the str and dict properties but haven’t had any luck with it.

I’m new to Python and I might not be aware of all the available built-in options.

Thanks

SS

On the server you can do:

rows = list(dict(r) for r in app_tables.my_table.search())

on the client (at present) you will get a suspension error - so do

rows = list(dict(list(r)) for r in app_tables.my_table.search())

Thanks @stucork.

Tried the server-side solution and it worked.

However some of my fields in the dictionary is still coming back as <LiveObject: anvil.tables.Row> for the values as they have singleRow reference to other fields.

Any tricks on how to expand those columns?

Do I just have to build this out manually in the for loop and expand each column and create the dictionary manually?

SS

you might like this:

But otherwise, yeah - you’ll also have to do something similar for linked rows, since

rows = list(dict(r) for r in app_tables.my_table.search())

will only go one level deep…

To go a second level you could do something like:

def get_row_as_dict(row):
  return dict(row)

rows_as_dicts =[]
table_row_cols = ['column_w_table_row']
for row in app_tables.my_table.search():
  row_as_dict = get_row_as_dict(row)
  for col in table_row_cols:
    row_as_dict[col] = get_row_as_dict(row[col])
  rows_as_dicts.append(row_as_dict)
return rows_as_dicts

3 Likes

Looks like that Dynamic serialisation Anvil project makes use of Marshmallow which I believe is only available in the paid plan.

I’ll resort to writing my own inner for loop to handle columns like the example provided.

Thanks
SS

1 Like