Working With Shopify Data Using Postgres JSONB | Flair

Working With Shopify Data Using Postgres JSONB

Jan 29, 2016·6 min read

On this page

My focus this week is on the best way to store and manage data for my upcoming Best-Sellers Shopify app.

Shopify uses JSON to transport data to and from the Shopify API. My go to database these days is Postgres, which also has some great tools for storing and working with JSON data. This means Shopify and Postgres effectively "speak the same language".

In this post, I'll walk you through how I will be using Postgres's built-in JSONB data type to store and manage data for my upcoming Best-Sellers Shopify app.

This post will include:

Shopify and JSON

Shopify uses JSON (Javascript Object Notation) as the means of sending and receiving data throughout the Shopify API.

You can see some example Order JSON in last week's A Day in the Life of a Shopify Order post.

Postgres and JSON

The Postgres database has support for working with JSON via two different data types: json and jsonb:

Shopify JSON and Postgres JSONB - the perfect match

Shopify sends all of its API and Webhook data via JSON. Postgres supports tools for storing and working with JSON data. This means we can pass Shopify JSON data directly to Postgres without modification.

For my Best-Sellers app, I opted to choose JSONB over JSON since I really need some of the advanced querying capabilities provided by JSONB and I'm ok with the drawbacks such as slower updates and lack of whitespace.

An Example Shopify Order schema using JSONB

To demonstrate how to work with Shopify data using Postgres's JSONB data type, I'm going to show you a simple database schema that is very similar to the one I will be using for my app. Then in the next section of this post, I'll demonstrate a few queries for working with the JSONB data.

Here is an example Rails migration for a Shopify Orders table:

create_table :orders  do |t|
  t.integer  :shop_id, null: false
  t.bigint   :shopify_order_id, null: false # the shopify order id
  t.jsonb    :order_data
  t.timestamps
end

add_index :orders, :shop_id
add_index :orders, :shopify_order_id, unique: true
add_index :orders, :order_data, using: :gin

add_foreign_key :orders, :shops, on_delete: :cascadeCopied!

And here is the same Orders table in SQL:

Column      |            Type             |                      Modifiers
------------------+-----------------------------+-----------------------------------------------------
 id               | integer                     | not null default nextval('orders_id_seq'::regclass)
 shop_id          | integer                     | not null
 shopify_order_id | bigint                      | not null
 order_data       | jsonb                       |
 created_at       | timestamp without time zone |
 updated_at       | timestamp without time zone |
Indexes:
    "orders_pkey" PRIMARY KEY, btree (id)
    "index_orders_on_shopify_order_id" UNIQUE, btree (shopify_order_id)
    "index_orders_on_order_data" gin (order_data)
    "index_orders_on_shop_id" btree (shop_id)
Foreign-key constraints:
    "fk_rails_7e761c2e1b" FOREIGN KEY (shop_id) REFERENCES shops(id) ON DELETE CASCADECopied!

A few notes about the schema:

Querying Shopify Order data with JSONB

Now let's walk through a few examples so you can see how you can query this Order data stored as JSONB.

Select a count of all orders by financial_status

SELECT order_data->'financial_status' AS financial_status, COUNT(*)
FROM orders GROUP BY financial_status;

financial_status   | count
----------------------+-------
 "refunded"           |     1
 "pending"            |     1
 "paid"               |     1
 "partially_refunded" |     2
(4 rows)Copied!

Select a count of all orders between two dates

SELECT COUNT(*) FROM orders o
WHERE (o.order_data->>'processed_at')::timestamptz
BETWEEN '2016-01-01' AND '2016-02-01';

count
-------
     5
(1 row)Copied!

Select a sum of all line item quantities and prices, by product_id

SELECT li->'product_id' AS product_id, SUM((li->>'quantity')::int), SUM((li->>'price')::numeric)
FROM orders o, jsonb_array_elements(o.order_data->'line_items') li GROUP BY li->'product_id';

product_id | sum |  sum
------------+-----+--------
 null       |   5 |  49.95
 2938468037 |   5 | 210.00
 3461132549 |   5 |  99.95Copied!

Learn How to Double (2X) Your Shopify Sales with Product Labels

See the guide

Summary

Shopify and Postgres make a powerful combo. Since both Shopify and Postgres are able to work directly with JSON, there is no need to modify the data. This means nothing should get lost in translation when you are storing data from Shopify for an app.

Postgres provides some powerful tools for storing and querying JSON data. In particular, the JSONB data type provides both power and flexibility to manage JSON data within your app.

Related Guides

Best Shopify Apps for Clothing Stores in 2026\ \ \ Clothing and Shopify are a match made in e-commerce heaven.

Shopify Partner-friendly Apps\ \ \ Shopify recently announced they've put together a list of partner-friendly apps that allow free testing within development stores. These apps allow you to try out apps in a development store without any upfront costs.

Increase Your Shopify Sales by up to 275% with Flair

Learn more No thanks - I don't want to learn more