xnhdev
xnhdev•11mo ago

Nested JSON

Hi team, I've searched to the best of my capabilities regarding nested json support. The following is a sample JSON reply from an API, inside nextjs 14 (app router) , the idea is to write the response to Convex and subsequently reading it (reall response will include multiple elements in "items" array) API JSON Response
{
"total": 80,
"items": [
{
"description": "The best of the best",
"images": [
{
"height": 640,
"url": "https://cdn",
"width": 640
},
{
"height": 300,
"url": "https://cdn",
"width": 300
},
],
"name": "Nested JSON Example",
"owner": {
"display_name": "Nested JSON",
},
"id": "UUID",
}
},
// Not part of actual JSON, to illustrate second item below
{
"description": "The best of the best 2",
....
}
]
}
{
"total": 80,
"items": [
{
"description": "The best of the best",
"images": [
{
"height": 640,
"url": "https://cdn",
"width": 640
},
{
"height": 300,
"url": "https://cdn",
"width": 300
},
],
"name": "Nested JSON Example",
"owner": {
"display_name": "Nested JSON",
},
"id": "UUID",
}
},
// Not part of actual JSON, to illustrate second item below
{
"description": "The best of the best 2",
....
}
]
}
I used "Add Document" (testing) in the dashboard and pasted it in and it was successfully loaded as one document with the field "items" auto generated including a single array that includes the raw API JSON response above Field "items"
[
// the above raw response
]
[
// the above raw response
]
Few questions: 1. Is this a supported way to store a JSON reply? 2. if supported, can you read/query based on nested elements natively through Convex? for example returning something like
items[1].name = "Example JSON 2"
items[1].name = "Example JSON 2"
but natively 3. I can query all and use regular javascript to filter, maybe that's the way to do it, more of a design question, but was hoping to do it at the source 4. tried the following, which undoubtly wrong but couldnt come up with better way, tried different variations, which just return []
return await ctx.db.query("spotify_playlist")
.filter((q) => q.eq(q.field("items.items.name"), "Example JSON 2"))
.take(10)
return await ctx.db.query("spotify_playlist")
.filter((q) => q.eq(q.field("items.items.name"), "Example JSON 2"))
.take(10)
5. Lastly, please tell me if there is something fundamentally missing from my understading of web app/db flow Thanks!
3 Replies
xnhdev
xnhdevOP•11mo ago
Forgot to add, I could potentially nomalize the response instead of adding it as is i.e nested
lee
lee•11mo ago
1. Yes you can store it like that. If you want a more scalable way to store it, check out https://stack.convex.dev/relationship-structures-let-s-talk-about-schemas 2-4. If storing an array, you can do the filter in javascript, it works just like query.filter: https://stack.convex.dev/complex-filters-in-convex 5. The way you store data is up to you 😄 . There are tradeoffs for everything. Storing an array in a single document makes it less efficient to filter and limits the size of the array, but easier to return the whole response all at once.
Relationship Structures: Let's Talk About Schemas
In this post we’ll look at some patterns for structuring relationships in the Convex database.
Using TypeScript to Write Complex Query Filters
There’s a new Convex helper to perform generic TypeScript filters, with the same performance as built-in Convex filters, and unlimited potential.
xnhdev
xnhdevOP•11mo ago
Thanks @lee will look at these documents, appreciate the feedback!

Did you find this page helpful?