Next.js Discord

Discord Forum

Parse nested JSON or get rid of it?

Unanswered
Montezuma Quail posted this in #help-forum
Open in Discord
Avatar
Montezuma QuailOP
Hey;
i am coding my first app with Next.js or javascript in general. The app is used to assist around an existing CRM based on MS SQL Server. I am building a set of dashboards and tables for specific usecases. The information are spreaded oven a bunch of tables in the CRM.
I am relatively good at sql, so i wrote some views that give me exactly the information i need. I thought it would be clever to pack the results of joined tables into a json field via the "for json" command.
For example the table "contract" has a 1:n relation to the table "inquiry" and my query delivers (in theory) the following json:

[
{
contract: 12345,
amount: 45.34
inquiries: [
{
nr: 123,
desc: abc
},
{
nr: 456,
desc: def
},
]
},
....
]

BUT the SQL Server wraps the inner json in quotes. For my next-app this is just a string and i cannot use the information like contract.inquiries[0].nr or map over them.... 😦

Is there an easy way to parse this inner json so that i can map over the inner array of inquiries?
Or is my approach just bull*** and i should get rid of the inner json and query the inquiries for each contract separately?

Thanks

0 Replies