EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SQL Tutorial SQLAlchemy JSONB
 

SQLAlchemy JSONB

Updated March 16, 2023

SQLAlchemy JSONB

 

 

Introduction to SQLAlchemy JSONB

SQLAlchemy JSONB datatype is supported in sqlalchemy; it will help both json and jsonb data types. It includes all the operations supported in json; it also works the same on indexing operations. It also adds the additional operator who is not present in the data type of json. It does not detect the changes which were seen by json data type.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

What is SQLAlchemy JSONB?

In sqlalchemy jsonb, the custom deserializers and serializers are shared using the JSON class. It will serialize and deserialize by using the json_serializer and json_desrializer arguments. It will be specified by using the level of dialect using the create_engine method. It is a straightforward and convenient way of storing structured data in rows. Json and jsonb are similar; the only difference is that jsonb added more extra features on it. The json and jsonb both allow the nesting of all data types. Hstore data type only supports the data type of string and only allows one level of nesting, so using the jsonb data type is very useful.

Index operations will invoke by calling the expression using the bracket operator of python. It will return the expression object, which type is defaulted to the JSON. After defining the json type as default, the further jsonb instructions are called using the result type. We can say that index creation is more common when using sqlalchemy jsonb data type; it will return the scalar element, an integer type.

To provide access to data elements in the sqlalchemy jsonb data type, we are using the following data casters as follows:

  • Comparator.as_string: This data caster will return the string element. This data caster is very important in it.
  • Comparator.as_boolean: This data caster will return the Boolean element. This data caster is very useful in it.
  • Comparator.as_float: This data casters will return the float element. This data caster is very important in it.
  • Comparator.as_integer: This data caster will return the integer element. This data caster is very useful in it.

Above data, casters will be implemented by supporting the dialects to ensure that the data casters’ comparison will work as expected.

How to Create SQLAlchemy JSONB?

To create the sqlalchemy jsonb data type, we must first install the sqlalchemy package in our system. Without installing this package in our code, we cannot use the sqlalchemy jsonb data type. Unfortunately, the Sqlalchemy module is not coming when installing the python package in our system. Therefore, to use sqlalchemy, we need to install the sqlalchemy module using the pip command.

Below steps shows how to create sqlalchemy jsonb as follows:

1. In the first step, we install the sqlalchemy module using the pip command. We can install the sqlalchemy module in any operating system on which python is installed. In the below example, we are installing the sqlalchemy module as follows.

Code:

pip install sqlalchemy

Output:

SQLAlchemy JSONB 1

2. After installing all the modules, we open the python shell using the python command.

Code:

python

Output:

SQLAlchemy JSONB 2

3. After login into the python shell in this step, we are checking sqlalchemy package is installed in our system.

Code:

import sqlalchemy

Output:

SQLAlchemy JSONB 3

4. After checking whether or not the sqlalchemy package is installed, we import the following modules using the sqlalchemy package. We are importing the MetaData, create_engine, Column, Table, Integer, text, JSONB, and string packages using the sqlalchemy package. We are importing all the modules while executing the separate command. We are importing all the modules by using the import keyword.

Code:

from sqlalchemy import MetaData
from sqlalchemy import create_engine
from sqlalchemy import Column
from sqlalchemy import Table
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy import Text
from sqlalchemy.dialects.postgresql import JSONB

Output:

SQLAlchemy JSONB 4

5. After importing all the modules in this step, we are giving the sqluri of the database server, and also, we are calling the create_engine method to create the table into the database server. Also, we are defining the MetaData for the SQL engine.

Code:

py_uri = 'sqlite:///db.sqlite'
py_engine = create_engine (py_uri)
sql_meta = MetaData (py_engine)

Output:

SQLAlchemy JSONB 5

6. After defining the sql_uri and calling the create_engine method in this step, we create multiple tables.

In the below example, we are creating a single table name as py_stud1.

Code:

sql_stud1 = Table ('EX1', py_meta,
Column ('id', Integer, primary_key=True),
Column ('name', JSONB))

Output:

we are creating the multiple table

7. After creating the table, we call the create_all method to create the table into the database server.

Code:

py_meta.create_all ()

Output:

we are calling create_all method

Examples of SQLAlchemy JSONB

Different examples are mentioned below:

Example #1

In the below example, we create two tables with sqlalchemy jsonb data type. In the below example, we have imported the MetaData, create_engine, Column, Table, integer, jsonb, and string modules.

After importing the module, we provide the database url and call the create_engine method; then, we define the MetaData method. Then we create the two and give the column data type as jsonb.

Code:

from sqlalchemy import MetaData
from sqlalchemy import create_engine
from sqlalchemy import Column
from sqlalchemy import Table
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy import Text
from sqlalchemy.dialects.postgresql import JSONB
jsonb_uri = 'sqlite:///db.sqlite'
jsonb_engine = create_engine (jsonb_uri)
jsonb _meta = MetaData (jsonb_engine)
jsonb_stud1 = Table ('EX1', jsonb_meta,
Column ('stud_id', Integer, primary_key=True),
Column ('stud_name', JSONB))
Jsonb_stud2 = Table ('EX2', jsonb_meta,
Column ('stud_id', Integer, primary_key=True),
Column ('stud_addr', JSONB))
jsonb_meta.create_all ()

Output:

creating two tables

Example #2

In the below example, we create the single table by providing column data type as jsonb.

Code:

from sqlalchemy import MetaData
from sqlalchemy import create_engine
from sqlalchemy import Column
from sqlalchemy import Table
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy import Text
from sqlalchemy.dialects.postgresql import JSONB
jsonb_uri = 'sqlite:///db.sqlite'
jsonb_engine = create_engine (jsonb_uri)
jsonb_meta = MetaData (jsonb_engine)
jsonb_stud = Table('EX1', jsonb_meta,
Column ('id', Integer, primary_key=True),
Column ('name', JSONB))
jsonb_meta.create_all ()

Output:

SQLAlchemy JSONB 9

Conclusion

The sqlalchemy jsonb will avoid creating new tables and their relationships; it will perform the use cases after setting up sqlalchemy jsonb properly. Furthermore, Sqlalchemy supports the jsonb data type while using the jsonb; we can add support for the same into the models of ORM.

Recommended Articles

This is a guide to SQLAlchemy JSONB. Here we discuss the introduction, how to create SQLAlchemy JSONB? and examples. You may also have a look at the following articles to learn more –

  1. SQL ORDER BY DESC
  2. SQL EXECUTE
  3. PL/SQL NOT EQUAL
  4. SQL NOT IN

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW