🧚 Listen! Beekeeper Studio is a fast, modern, and open source SQLite GUI Download
January 25, 2023 By Matthew Rathbone

JSON, or JavaScript Object Notation, is a format for storing and exchanging data. It is a lightweight, text-based, and human-readable format that is easy for machines to parse and generate. In SQLite, JSON can be stored and queried in a few different ways.

Using the JSON1 Extension

One way to store JSON in SQLite is to use the JSON1 extension, which provides functions for encoding and decoding JSON. To use the JSON1 extension, you first need to enable it by running the following command:

SELECT load_extension('/path/to/json1/extension');

Once the extension is enabled, you can create a table that has a column of type JSON using the following SQL statement:

CREATE TABLE users (
  id INTEGER PRIMARY KEY,
  data JSON
);

Inserting JSON Data

To insert a JSON value into this table, you can use the json_encode function provided by the JSON1 extension, like this:

INSERT INTO users (id, data) VALUES (1, json_encode({
  "name": "Alice",
  "age": 25,
  "email": "alice@example.com"
}));

Querying JSON Data

To query this table, you can use the json_extract function, which allows you to extract values from the JSON column by specifying a JSON path. For example, to get the email address of the user with id 1, you can use the following query:

SELECT json_extract(data, '$.email') AS email
FROM users
WHERE id = 1;

Store JSON in BLOB

Another way to store and query JSON in SQLite is to use the BLOB type. In this case, you can create a table with a BLOB column like this:

CREATE TABLE users (
  id INTEGER PRIMARY KEY,
  data BLOB
);

Inserting JSON into a BLOB Column

To insert a JSON value into this table, you can use the json_encode function to encode the value as a string, and then use the hex function to convert it to a BLOB, like this:

INSERT INTO users (id, data) VALUES (1, hex(json_encode({
  "name": "Alice",
  "age": 25,
  "email": "alice@example.com"
})));

Querying JSON in a BLOB Column

To query this table, you can use the JSON_EXTRACT function provided by the JSON1 extension, but you need to convert the BLOB value back to a string using the unhex function, like this:

SELECT json_extract(unhex(data), '$.email') AS email
FROM users
WHERE id = 1;

Summary

In conclusion, SQLite provides two ways to store and query JSON values: using the JSON1 extension and using the BLOB type. Both approaches have their own advantages and disadvantages, and which one you choose will depend on your specific needs.

If you need to convert JSON data to SQL INSERT statements, check out our free JSON to SQL Converter tool.

Beekeeper Studio Is A Free & Open Source SQLite GUI

This is precisely what I needed. The UI is snappy and clean, works as I’d like and expect, and just gets the job done. It’s the perfect tool for working on my SQLite databases. - Andy

Beekeeper Studio is a fast, intuitive and easy to use SQLite client. It supports loads of other databases too, and works great on Windows, Mac and Linux.

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

What SQLite Users Say About Beekeeper Studio

Tagser Programmer in training(No title or employment)

I never truly understood how model design works until I started using this to query and view database! I am grateful for this in my journey. I recommend it for someone learning Django + sqlite since that is only what I have used it for so far.

George Wright

Two key things. First, connectivity. I don’t have to use DB Browser for SQLite, MySQL Workbench for MySQL, psql for PostgreSQL. Beekeeper works with them all. Second: AI. The AI feature compiles English into SQL.

Ready to Improve Your SQLite Workflow?

download Download Free