Question
· Aug 30, 2019

Assign query output to Class object

 

I want to assign below query result into my Person class which have Name,DOB properties, and then  convert into JSON.

SELECT Name,DOB FROM Sample.Person
Discussion (8)0
Log in or sign up to continue

A few more details about your exact use case would help me give a better answer here.  As for the exact questions you asked, here's an example of the syntax you can use to write a query as part of a class:

Query Orgs() As %SQLQuery(CONTAINID = 1)
{
SELECT ID As OrgID,Name,DisplayName FROM Org ORDER BY Name
}

As for getting the query result as JSON, I asked a similar question a little while ago, and this discussion was very useful:

https://community.intersystems.com/post/how-do-i-return-json-database-sq...

Now, if your real question is something along the lines of "I want to write a REST endpoint that gives me back the result of a class query in Person as JSON", then unfortunately that's a non-trivial problem.  The fastest way would be to write some endpoint, like /basicpersoninfo, and have the REST handler route requests for that endpoint to a method that uses JSON_ARRAYAGG and JSON_OBJECT and dynamic SQL  (discussed in that other thread) to write out the results of that query in JSON.