Best way to look for text in a deeply nested JSON?
I have a JSON field in a table. Which holds the metadata for the row. So what will be in the JSON is kind of unpredictable. Because it'll store tiptap editor json, other json etc.,
Here are the few examples
Example 1:
Example 2:
Example 3:
What's the efficient way to handle the search? Example search term is "Rocket"
Solution:Jump to solution
Okay, I did this.
``
const postsWithMeta = await db.$queryRaw<Posts[]>
SELECT p.*
FROM "Posts" p...7 Replies
Hi @Naveen MC
Since the JSON field has an unpredictable structure, i think using database specific JSON search function through either raw query of typedsql should work. This would also allow search across all possible locations in the json field.
Hi @RaphaelEtim Thanks for you input. Is there any resources I can look at.
Hi @Naveen MC
For PostgreSQL, you could use the
jsonb_path_query_array
function, which allows you to search across all possible locations in the JSON field. For example:
This query searches for the term "Rocket" case-insensitively across all string values in the JSON structure, regardless of their location or nesting level.So doing prisma raw SQL query will help?
Solution
Okay, I did this.
And it seems to be working
Thanks for sharing your solution 🚀
Hope it'll help someone ✨