xnhdev
xnhdev
CCConvex Community
Created by xnhdev on 4/8/2024 in #support-community
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!
4 replies