One of the most well-liked relational database management systems (RDBMS) in the world, PostgreSQL is quite strong and allows developers access to a wide range of complex capabilities. One of the most useful features provided by Postgres is the support for JSON data types, together with the ability to write SQL. PostgreSQL is capable of CRUD activities like reading and writing JSONB data (binary JSON format). The greatest option for working with relational databases is SQLAlchemy, one of the most well-liked Python ORM libraries. This gives us the ability to interface with the database and Python, as well as operate with the JSONB data format in PostgreSQL.
JSONB
A PostgreSQL data type called JSONB stores JSON (JavaScript Object Notation) data in binary format, which uses less storage space and processes more data quickly and efficiently than JSON stored in text format. A well-liked Python library for working with relational databases, such as PostgreSQL, is SQLAlchemy.
You may use the strength and agility of JSON data in the Postgres database while still using Python to query and update the JSON data by combining JSONB with SQLAlchemy. Postgres is a powerful open-source database and is widely used. We’ll show you how to use JSONB with SQLAlchemy by going over a few examples. We will talk about the basics of making a JSONB column, running JSON queries, and editing JSON data.
Postgres stores data that is written in JSON format called JSONB, which is a binary format. This allows searching both index and nested JSON items within Postgres. We will use an ORM layer (SQLAlchemy) which makes it easier to interact and easier to work with JSONB data in PostgreSQL. SQLAlchemy uses JSON data and defines a JSONB column within our database. It also defines JSONB columns in the database schema using SQLAlchemy’s provision of a JSONB type. In addition to that, we are able to use the JSONB column to retrieve the data and carry out SQL operations on it.
In python, we need to use the PostgreSQL dialect of SQLAlchemy using the python package and wrapper psycopg2, which provides additional functionality for working with PostgreSQL-specific data types like JSONB. We also need to install the psycopg2 driver, which is a PostgreSQL adapter for Python.
Required Modules:
pip install psycopg2
Stepwise Implementation:
For the purpose of this article, we will be using the following database schema:
Step 1: Create a table in the existing data in Postgres and define the column in the table as JSONB.
Python
from sqlalchemy import create_engine, Table, Column, Integer, MetaData, JSON
from sqlalchemy.dialects.postgresql import JSONB, insert
engine = create_engine('postgresql://vikadmin:dbpass@localhost:5432/vikashdb')
metadata = MetaData()
# reflect the database schema to the metadata
metadata.reflect(bind=engine)
users = Table('users', metadata,
Column('id', Integer, primary_key=True),
Column('data', JSONB),
extend_existing=True
)
metadata.create_all(engine)
# get the table object
table_name = 'users'
table = metadata.tables[table_name]
# print the table schema
print(table.__repr__())
Here, we have created a table named users with two columns: id and data. The data column is of type JSONB, which means it can store JSON data.

Output Image
Step 2: Now, let’s insert some data into this table:
Python
data = {
'name': 'Vik',
'age': 30,
'job_title': 'Data Scientist'
}
insert_stmt = insert(users).values(data=data)
conn = engine.connect()
conn.execute(insert_stmt)
conn.commit()
In this example, we have inserted a dictionary of JSON data into the data column of the user’s table. We used the insert function from the sqlalchemy.dialects.postgresql module to construct the SQL statement.
Step 3: To query this data, we can use the select function:
Python
# Fetch the data
select_stmt = users.select()
result = conn.execute(select_stmt)
row = result.fetchone()
print(row)
Output

Output Image
Step 4: We can also update the JSON data in the data column:
In this example, we have updated the data column for the row with an id equal to 1, setting the job_title key to Software Engineer.
Python
# Update data
update_stmt = users.update().where(users.c.id == 1).values(
data={'job_title': 'Software Engineer'})
conn.execute(update_stmt)
conn.commit()
Step 4: Selecting Specific JSONB Fields with SQLAlchemy 2.0:
In this example we explained the syntax to select Specific JSONB Fields.
TableName.jsonb_column_name["target_dict"]["target_subdict"]
is used to access a nested field within the JSONB column..where(TableName.id_column == target_id_value)
is used to filter the rows based on an id_column
.
Python
from sqlalchemy import select
from your_model import TableName
stmt = select(TableName.jsonb_column_name["target_dict"]["target_s\
ubdict"]).where(TableName.id_column == target_id_value)
result = session.execute(stmt).fetchall()
Step 5: Parsing Results
By default, SQLAlchemy parses JSONB columns using the JSONB
type with astext_type=Text()
. This means the JSONB data is treated as text when fetched, allowing it to be converted into a Python dictionary (or other JSON-compatible data structures).
Python
from sqlalchemy.dialects.postgresql import JSONB
# Assuming your_model is already defined
TableName.jsonb_column_name = Column(JSONB(astext_type=Text()))
When you query this column, the result can be treated as a tuple containing a dictionary (or JSON object). For example
Python
result = session.execute(stmt).fetchall()
for row in result:
jsonb_as_dict = row[0]
# Assuming the select statement only
# fetches the JSONB field
# Now jsonb_as_dict can be used as a dictionary
With SQLAlchemy, we are able to insert data that is formatted as JSONB into the database. We begin by constructing a JSONB object utilizing Python, and after that, we use SQLAlchemy to insert it into the database.
Example 2:
Python
from sqlalchemy import create_engine, Column, Integer, JSON
from sqlalchemy.orm import sessionmaker, declarative_base
from sqlalchemy.dialects.postgresql import JSONB, insert
engine = create_engine('postgresql://vikadmin:dbpass@localhost:5432/vikashdb')
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()
class Employee(Base):
__tablename__ = 'employees'
id = Column(Integer, primary_key=True)
info = Column(JSONB)
employee_info = {
'name': 'Vik Singh',
'age': 35,
'job_title': 'ML Engineer'
}
employee = Employee(id=15, info=employee_info)
session.add(employee)
session.commit()
In this demonstration, we will develop an Employee class that will map to the table titled “Employees” in the corresponding database table. We use a JSONB type for the info column’s definition. After that, a Python dictionary called employee info is constructed and populated with the relevant employee data. After adding the newly created instance to the session, we proceed to construct an instance of the Employee class using the employee info dictionary. Next, we make sure that the database is up to date by committing the modifications.
Similar Reads
SQLAlchemy Core - Joins
SQLAlchemy Core is a Python toolkit that enables developers to create complex database applications. It provides several features, one of which is the ability to join tables. Joining tables allows developers to retrieve data from multiple tables simultaneously, which is useful when the data is relat
3 min read
SQLAlchemy - Label
SQLAlchemy is a Python library used for working with relational databases. It provides an intuitive way to interact with databases and allows us to write database-independent code. In this article, we'll explore one of the powerful features of SQLAlchemy called label(), which is used to add labels t
4 min read
SQLAlchemy - Introduction
SQLAlchemy is basically referred to as the toolkit of Python SQL that provides developers with the flexibility of using the SQL database. The benefit of using this particular library is to allow Python developers to work with the language's own objects, and not write separate SQL queries. They can b
3 min read
SQLAlchemy ORM - Query
In this article, we will see how to query using SQLAlchemy ORM in Python. To follow along with this article, we need to have sqlalchemy and anyone database installed in our system. We have used the MySQL database for this article's understanding. Created a Profile table and a Students table: Here we
10 min read
SQLAlchemy db.session.query()
In SQLAlchemy, session.query() can be used as a filter in a query to specify criteria for which rows should be returned. This is done using the expression module and the filter method of the query object. The expression module allows you to create an expression that can be used in a query. This can
5 min read
Django ORM vs SQLAlchemy
For relational database newbies who are Python developers, Django ORM and SQLAlchemy are two heavyweights worth considering. As Object-Relational Mappers (ORMs) they act as middlemen between Pythonic objects and database tables thereby simplifying how we interact with data. Nevertheless making choic
10 min read
SQLAlchemy Core - Conjunctions
SQLAlchemy is a popular Python programming SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL in a Pythonic way. SQLAlchemy ORM or object-relational mapper is a component that provides an abstraction layer over the SQL database which make
6 min read
SQLAlchemy filter by json field
In this article, we will be discussing the SQLAlchemy filter by JSON field using Python. Before we begin the introduction, to the topic we will be discussing the basics of how to filter by JSON fields using a few examples and a brief introduction to SQLAlchemy, JSON, and JSON fields. Required Packag
5 min read
Floor division in SQLAlchemy
In this article, we will see how to perform floor division in SQLAlchemy against a PostgreSQL database in python. Floor division is performed in different methods using different functions. Such kinds of mathematical operations are database-dependent. In PostgreSQL, floor division is performed using
2 min read
SQLAlchemy Core - Functions
SQLAlchemy provides a rich set of functions that can be used in SQL expressions to perform various operations and calculations on the data. SQLAlchemy provides the Function API to work with the SQL functions in a more flexible manner. The Function API is used to construct SQL expressions representin
7 min read