Mechanism for turning nested JSON into foreign keys / many-to-many
See original GitHub issueThe 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:
- Created 4 years ago
- Reactions:3
- Comments:14 (12 by maintainers)
Top GitHub Comments
So maybe something like this:
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.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#”.