109 Rows for 17 Products: What Happens to Nested JSON When You Load an API
When we walked our Shopify setup guide against a real store, the run finished and reported 109 rows . The store had 17 products and 3 customers . Nothing was duplicated and nothing went wrong. Those 109 rows are exactly what the store holds, spread across seven tables instead of two. Load any API that returns nested JSON and you will see a number like this on your first run. It is worth…
When a Shopify API is loaded, the resulting data is spread across seven tables rather than a single flat table. This happens because each product contains lists of variants, images, and options, which in turn contain their own lists. To store this nested structure, the loading library, dlt, creates separate tables for each list.
The resulting number of 109 rows comes from the sum of all these tables, excluding the bookkeeping tables dlt creates. For example, a store with 17 products and 3 customers has 109 rows: 17 products, 26 product variants, 18 product images, 17 product options, 17 option values, 3 customers, and 2 customer addresses.
The nested nature of the data is evident in the table names, which reflect their hierarchical structure. For instance, 'products__variants' represents the list of variants for each product. The double underscore separates the path to each list within a table.
The Rows figure for an upload is the sum of all the data written to the tables, not just the number of products. This figure is useful for confirming that data was moved during the load process, but it does not give the exact number of products. When reconciling data, it's better to check each table individually.
dlt adds additional columns to the nested tables to maintain the relationships between them. These columns include _dlt_id, which is a unique key for each row across all tables, _dlt_parent_id, which points to the parent row for a nested table, and _dlt_list_idx, which represents the position of an item in its original list.
After a successful upload, the nested tables are recognized as dbt sources, allowing for easy transformation and querying. The schema for these sources is derived from the upload name, with whitespace replaced by underscores and the entire string converted to lowercase.
To join nested tables in a transformation, you can use the _dlt_parent_id column to link each child table back to its parent. This approach works regardless of how the vendor structures the data, as dlt includes the necessary information for all nested tables.
In summary, when loading a nested API like Shopify, you can expect a number of tables that is greater than the number of top-level objects. Understanding the structure of these tables, including the _dlt_parent_id and _dlt_list_idx columns, is crucial for accurate reconciliation and transformation in downstream processes.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to