question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mechanism for turning nested JSON into foreign keys / many-to-many

See original GitHub issue

The GitHub JSON APIs have a really interesting convention with respect to related objects.

Consider https://api.github.com/repos/simonw/sqlite-utils/issues - here’s a truncated subset:

  {
    "id": 449818897,
    "node_id": "MDU6SXNzdWU0NDk4MTg4OTc=",
    "number": 24,
    "title": "Additional Column Constraints?",
    "user": {
      "login": "IgnoredAmbience",
      "id": 98555,
      "node_id": "MDQ6VXNlcjk4NTU1",
      "avatar_url": "https://avatars0.githubusercontent.com/u/98555?v=4",
      "gravatar_id": ""
    },
    "labels": [
      {
        "id": 993377884,
        "node_id": "MDU6TGFiZWw5OTMzNzc4ODQ=",
        "url": "https://api.github.com/repos/simonw/sqlite-utils/labels/enhancement",
        "name": "enhancement",
        "color": "a2eeef",
        "default": true
      }
    ],
    "state": "open"
  }

The user column lists a complete user. The labels column has a list of labels.

Since both user and label have populated id field this is actually enough information for us to create records for them AND set up the corresponding foreign key (for user) and m2m relationships (for labels).

It would be really neat if sqlite-utils had some kind of mechanism for correctly processing these kind of patterns.

Thanks to jq there’s not much need for extra customization of the shape here - if we support a narrowly defined structure users can use jq to reshape arbitrary JSON to match.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:14 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
simonwcommented, Jun 13, 2019

So maybe something like this:

curl https://api.github.com/repos/simonw/datasette/pulls?state=all | \
  sqlite-utils insert git.db pulls - \
    --flatten=base \
    --flatten=head \
    --extract=user:users:id \
    --extract=head_repo.license:licenses:key \
    --extract=head_repo.owner:users \
    --extract=head_repo
    --extract=base_repo.license:licenses:key \
    --extract=base_repo.owner:users \
    --extract=base_repo

Is the order of those nested --extract lines significant I wonder? It would be nice if the order didn’t matter and the code figured out the right execution plan on its own.

0reactions
izzuescommented, Jun 29, 2022

Have you seen MakeTypes? Not the exact same thing but it may be relevant.

And it’s inspired by the paper “Types from Data: Making Structured Data First-Class Citizens in F#”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Convert a nested JSON string to Pandas dataframes (and add ...
The easiest thing to do is read in the csv and update the JSON data with the extra information. There's a lot of...
Read more >
Many-To-Many Relationships In FastAPI - GormAnalysis
In this tutorial, I cover multiple strategies for handling many-to-many relationships using FastAPI with SQLAlchemy and pydantic.
Read more >
Database Design Course by Caleb Curry - Paul Apivat
Physically draw out the lines and relationships (cardinality) one-to-one one-to-many ('many' side is the Foreign Key) many-to-many (break ...
Read more >
Briefly describe how to store complex relational data in ...
In the traditional database, the description of the data relationship is nothing more than three, one-to-one, one-to-many and many-to-many relationship.
Read more >
Convert a row to a nested JSON document containing all ...
I need to keep this trigger function as generic as possible because I plan to use it on all tables. One level of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found