Written by

Question Vivek Nayak · 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

Comments

John Kumpf · Aug 30, 2019

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,DisplayNameFROM OrgORDER 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-s…

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.

0
Eduard Lebedyuk  Aug 31, 2019 to John Kumpf

Starting from 2019.2 there's a %JSON.Adapter, for automatic bidirectional JSON<>Object transformation.

0
Neerav Verma  Sep 1, 2019 to John Kumpf

It doesn't seem to work on 2019.0

0
Eduard Lebedyuk  Sep 1, 2019 to Neerav Verma

What doesn't work in 2019.0?

Also do you mean 2019.1.0?

0