I have a table with a field of type JSON. The JSON field contains a JSON object with an array. How do I query the length of this array for each row?
class Post(Base):
__tablename__ = 'posts'
id = Column(Integer, primary_key=True)
items = Column(JSON)
Example rows in this table:
id: 1, items: [2,3,5]
id: 2, items: [2,3,5, 8, 12]
id: 3, items: [2,5]
id: 4, items: []
Expected result:
1 3
2 5
3 2
4 0
Is there something like this:
session.query(Post.id, len(Post.items))
Note: The JSON field contains an array of complex objects, I have used integers here only for the purpose of illustration.