Concepts / Working with JSON in Python

Working with JSON in Python

A JSON document can contain both entities and information about how those entities are related.

  • Programming

From Nested Data to Related Rows

A JSON document can contain both entities and information about how those entities are related. Loading that data into a relational database requires distributing it across two entity tables and one junction table. The central task is not merely reading the JSON: it is preserving the relationships while converting the data into database rows.

The loading process has three connected responsibilities. First, extract the information for each entity from the JSON. Second, make sure each entity exists in its own table and obtain that row's primary key. Third, place the two primary keys into a junction table to represent their relationship. This three-table distribution is the database pattern used for many-to-many data loading.

JSON documententity fields andrelationshipsEntity table Aentity rowsJunction tabletwo primary keysEntity table Bentity rowsentity fieldsentity fieldsprimary keyprimary key
How do entity fields and nested relationship data move from JSON into two entity tables and one junction table?

Tracing Entity Creation

Worked example

Ensuring Two Related Entities Exist

A JSON record identifies one item of entity type A and one item of entity type B, along with a relationship between them. How should the loader prepare the database rows?

  1. 1
    Identify the entities

    Read the JSON fields that describe the entity of type A and the entity of type B. Treat the relationship information separately from the entity information.

  2. 2
    Ensure entity A exists

    Use INSERT OR IGNORE for entity A, then immediately use SELECT to retrieve its primary key.

  3. 3
    Ensure entity B exists

    Use INSERT OR IGNORE for entity B, then immediately use SELECT to retrieve its primary key.

  4. 4
    Prepare the relationship

    The two retrieved primary keys are now the identifiers needed for the junction-table row.

Result

The loader has ensured that both entity records exist and has obtained both primary keys before attempting to store their relationship.

Entity datafrom JSONINSERT OR IGNOREensure record existsNew recordinsertedSELECTretrieve primary keyPrimary keyready for relationshipExisting recordignoredentity valuesnot presentalready presentcontinuecontinuereturns
What happens when an entity is new or already exists, and how is its primary key retrieved in both cases?
Key point

The SELECT step is required whether INSERT OR IGNORE inserted a new record or ignored an existing one. Retrieve the primary key immediately after INSERT OR IGNORE so it is available for the next database operation.

Linking the Junction Row

A junction table connects the two entity tables by storing the primary key from each one. The loader should therefore retrieve both entity keys before inserting the relationship. The relationship row is built from identifiers, not from repeating the full entity data.

Entity Aprimary key: key AEntity Bprimary key: key BJunction rowkey A + key Bkey Akey B
How do the primary keys retrieved from two related entity tables become one junction-table row?
Worked example

Building One Relationship

After the loader retrieves the primary key for entity A and the primary key for entity B, what information belongs in the junction table?

  1. 1
    Keep the first key

    Use the primary key retrieved from the entity A table.

  2. 2
    Keep the second key

    Use the primary key retrieved from the entity B table.

  3. 3
    Insert the pair

    Insert the two keys together as the representation of the relationship in the junction table.

Result

The junction table contains a row linking the two existing entity records through their primary keys.

Keeping Relationships Idempotent

The same relationship may appear more than once during loading. A unique constraint identifies duplicate relationship entries, while INSERT OR REPLACE handles the junction-table insertion without leaving duplicate relationships. Together, the constraint and the insertion pattern support idempotent loading: repeating the same relationship operation does not create an additional duplicate relationship.

Relationship pairfirst junction entryUnique constraintduplicate identifiedINSERT OR REPLACEhandle junction insertOne relationshipno duplicate entryRepeated pairsame relationshipstored pairsame pairduplicate relationshipmaintains relationship
How does a unique constraint identify a duplicate relationship, and what changes when INSERT OR REPLACE handles it?

A Related Parsing Model

The source material also describes a related parsing operation for XML. ET.fromstring converts an XML string into a tree structure. That tree changes flat text into a navigable and queryable object, so parent and child elements can be accessed through the resulting structure.

XML stringflat inputRoot elementtree entryChild elementnested dataChild elementnested dataET.fromstringcontainscontains
What tree structure is created from an XML string, and how are parent and child elements represented afterward?
Parsing taskResult described in the source
JSON loading for database insertionEntity and relationship information is distributed across two entity tables and one junction table
XML conversion with ET.fromstringAn XML string is converted into a navigable, queryable tree structure

Both tasks transform structured text into a form that can be navigated for later work, but the source describes different target structures.

Mistakes in Relationship Loading

  • Common mistake

    Inserting a relationship before retrieving both entity primary keys.

    What it looks like

    The loader identifies two related entities but does not complete the SELECT step for one of them.

    Why it fails

    The junction table must link the two entities through their primary keys.

    Do this instead

    Complete INSERT OR IGNORE followed immediately by SELECT for each entity before inserting the junction-table relationship.

  • Common mistake

    Assuming INSERT OR IGNORE alone provides the primary key needed later.

    What it looks like

    The loader ensures that an entity exists but skips the SELECT operation.

    Why it fails

    The source specifically requires retrieving the primary key immediately after INSERT OR IGNORE.

    Do this instead

    Use SELECT after INSERT OR IGNORE for both new and existing records.

  • Common mistake

    Using a junction-table insert without duplicate protection.

    What it looks like

    The same pair of entity keys is inserted into the junction table more than once.

    Why it fails

    Repeated relationship data can create duplicate relationship entries.

    Do this instead

    Use a unique constraint together with INSERT OR REPLACE for junction-table inserts.

Practice the Loading Sequence

MEDIUM

A JSON record contains information for entity A, information for entity B, and a relationship connecting them. Describe the database-loading sequence in the correct order. Include the operation used for each entity, the point at which each primary key is retrieved, and the operation used for the junction-table relationship.

Hints
  • Separate entity processing from relationship processing.
  • For each entity, place SELECT immediately after INSERT OR IGNORE.
  • Use both retrieved primary keys for the junction-table operation.
  • Account for repeated relationships with a unique constraint and INSERT OR REPLACE.
  1. 1
    A reliable JSON-to-database loader follows a traceable sequence: identify the entities and relationship, ensure each entity exists with INSERT OR IGNORE, retrieve each primary key immediately with SELECT, and use the two keys to populate the junction table. A unique constraint and INSERT OR REPLACE prevent duplicate relationship entries and support idempotent loading.

Key Takeaways

  • Many-to-many JSON data is distributed across two entity tables and one junction table.
  • INSERT OR IGNORE handles both new and existing entity records without errors.
  • SELECT must retrieve each entity primary key immediately after INSERT OR IGNORE.
  • The junction table links two entities by storing their primary keys.
  • A unique constraint combined with INSERT OR REPLACE prevents duplicate relationships and supports idempotent loading.

Related concepts

More concepts starting with W