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.

A Database Manager That Is Modern, Fast, & Easy To Use

Tried a few tools. Beekeeper was the only one that I found that felt right. Most had a very 1990's feel to them - Allan

I built Beekeeper Studio because, like Allan, I wanted something more intuitive and modern than all the existing clunky apps I could find. My customers agree - they love using Beekeeper and they tell me every day! Give it a try, I bet you'll like it too.

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

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.