January 25, 2023 By Matthew Rathbone

SQLite is a popular database management system that uses a file to store data and database state. It is often used for smaller projects or as an embedded database for applications. One of the data types that SQLite supports is a BLOB (binary large object), which can be used to store large amounts of binary data, such as images or documents.

BLOBs can also be used to store JSON data. JSON (JavaScript Object Notation) is a text-based data interchange format that is used to store and transmit data structures, such as lists and dictionaries. It is often used in web applications and APIs to transmit data between the server and the client.

Code examples below are in Python

First make a byte array

To store JSON in a BLOB column in SQLite, you will need to first convert the JSON data to a binary format. This can be done using the json.dumps() function in Python, which converts a Python object (such as a dictionary or list) to a JSON string. The resulting string can then be converted to a binary format using the bytes() function. For example:

import json

data = {'name': 'John', 'age': 30, 'city': 'New York'}
json_data = json.dumps(data)
binary_data = bytes(json_data, 'utf-8')

Insert byte array into the table

Once the JSON data has been converted to a binary format, it can be inserted into the database using an INSERT statement and the BLOB data type. For example:

import sqlite3

# Connect to the database
conn = sqlite3.connect('database.db')
cursor = conn.cursor()

# Insert the data into the table
cursor.execute('INSERT INTO users (json_data) VALUES (?)', (binary_data))

# Commit the changes and close the connection
conn.commit()
conn.close()

Querying the JSON requires retrieving the data first

To query the JSON data stored in a BLOB column, you will need to first retrieve the binary data from the database and convert it back to a JSON string using the bytes.decode() function. This can be done using a SELECT statement and the BLOB data type. For example:

import sqlite3

# Connect to the database
conn = sqlite3.connect('database.db')
cursor = conn.cursor()

# Select the data from the table
cursor.execute('SELECT json_data FROM users WHERE id=?', (1,))
binary_data = cursor.fetchone()[0]

# Convert the binary data to a JSON string
json_data = binary_data.decode('utf-8')

# Close the connection
conn.close()

Once you have the JSON string, you can use the json.loads() function in Python to convert it back to a Python object (such as a dictionary or list). For example:

import json

data = json.loads(json_data)
print(data)  # {'name': 'John', 'age': 30, 'city': 'New York'}

Wrap-up

Using a BLOB column to store JSON in SQLite can be a space-efficient way to store complex data structures. However, it is important to keep in mind that BLOBs are not indexed and are not searchable, so they may not be the best choice for storing large amounts of data or data that needs to be searched frequently.

In reality it’s probably better to use a TEXT column to store JSON data, thanks to the native JSON functions provided in SQLite.

Beekeeper Studio Is A Free & Open Source Database GUI

Best SQL query & editor tool I have ever used. It provides everything I need to manage my database. - ⭐⭐⭐⭐⭐ Mit

Beekeeper Studio is fast, intuitive, and easy to use. Beekeeper supports loads of databases, and works great on Windows, Mac and Linux.

Beekeeper's Linux version is 100% full-featured, no cut corners, no feature compromises.