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
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"
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 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 []
5. Lastly, please tell me if there is something fundamentally missing from my understading of web app/db flow
Thanks!3 Replies
Forgot to add, I could potentially nomalize the response instead of adding it as is i.e nested
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.
Thanks @lee will look at these documents, appreciate the feedback!