Use PostgreSQL data type (jsonb) from Python

I investigated how to use the data side peculiar to PostgreSQL from Python, so I will record it.

This is about the jsonb type.

This content is an excerpt of the content presented at the 51st Nagaoka IT Developer Study Group. The slide is here.

The Java version is here.

Use PostgreSQL from Python

利用方法

When using PostgreSQL from an application, use it via the DB API. DB API has psycopg2 as one of the implementations in the specification. psycopg2 uses libpq, the official PostgreSQL C client library, internally.

Source code

https://github.com/civic/postgresql-python-json

Use jsonb

id info
1 {"a": 100, "b": "Hello"}
2 {"a": 200, "b": "World"}
CREATE TABLE json_test(
  id integer,
  info jsonb
)

Using jsonb from low-level API (psycopg2)

SELECT

cur.execute(
"SELECT info FROM json_test WHERE id=1"
)
row = cur.fetchone()
print(row[0])
# {'b': 'Hello', 'a': 100}

If you get the json column, you can get it with dict (Python dictionary type).

INSERT (1)

from psycopg2.extras import Json

#Updated with json object
cur.execute(
  "INSERT INTO json_test(info) VALUES(%s)",
  [ 
    Json({"a":30, "b": "update"})
  ]
)

Set the parameters in the Json object provided by psycopg2. Use psycopg2.extras.Json. dict → Json.

INSERT (2)

import json

#Convert dict to string and pass it to SQL
cur.execute(
  "INSERT INTO json_test(info) VALUES(jsonb(%s))",
  [
    json.dumps({"a":30, "b": "update"}) //dict→str
  ]
)

A pattern that sets parameters as a JSON string and casts them to jsonb in an SQL statement. In this case, it can be used in the interface of DB API.

Using jsonb from high level API (SQLAlchemy)

SELECT / INSERT

from sqlalchemy.dialects.postgresql import JSONB

class JsonTest(Base):
    __tablename__ = 'json_test'
    id = Column(Integer, primary_key=True)
    info = Column(JSONB)  #JSONB type columns and declarations
    ...

For SQLAlchemy, which provides OR mapper functionality, the postgresql dialect can be used for column definitions.

Recommended Posts

Use PostgreSQL data type (jsonb) from Python
[Note] Get data from PostgreSQL with Python
Use thingsspeak from python
Use fluentd from python
Use MySQL from Python
Use MySQL from Python
Use BigQuery from python.
Use mecab-ipadic-neologd from python
Use MySQL from Anaconda (python)
Python data type summary memo
Image data type conversion [Python]
Use e-Stat API from Python
[Python] Use JSON format data as a dictionary type object
Python: Exclude tags from html data
Use Stanford Core NLP from Python
Hit treasure data from Python Pandas
Get data from Quandl in Python
Read and use Python files from Python
Forcibly use Google Translate from python
Use kabu Station® API from Python
Use Azure Blob Storage from Python
Use the Flickr API from Python
Use fastText trained model from Python
Use PostgreSQL with Lambda (Python + psycopg2)
Use Google Analytics API from Python
Receive textual data from mysql with python
Python: Reading JSON data from web API
Use machine learning APIs A3RT from Python
I want to use jar from python
[Python] Use string data with scikit-learn SVM
Use Google Cloud Vision API from Python
[Python] Web application from 0! Hands-on (4) -Data molding-
How to use "deque" for Python data
Use Django from a local Python script
Use C ++ functions from python with pybind11
Study from Python Hour6: Frequently used data types: tuple type, set type, dictionary type
Connect to postgreSQL from Python and use stored procedures in a loop.
Get time series data from k-db.com in Python
[Python] How to use two types of type ()
Python numeric type
Firebase: Use Cloud Firestore and Cloud Storage from Python
Study from Python Hour7: How to use classes
Data analysis python
First python (data type, control statement, function definition)
[Python] How to read data from CIFAR-10 and CIFAR-100
[Bash] Use here-documents to get python power from bash
Wrap C with Cython for use from Python
sql from python
Use PIL in Python to extract only the data you want from Exif
Use Python in your environment from Win Automation
[Python] Flow from web scraping to data analysis
MeCab from Python
I want to use ceres solver from python
Python2 string type
Python # string type
Let's use different versions of SQLite3 from Python3!
Wrap C ++ with Cython for use from Python
Use the nghttp2 Python module from Homebrew from pyenv's Python
Use Tor to connect from urllib2 [Python] [Mac]
Python: Use zipfile to unzip from standard input
[python] Read data