User bio
404 bio not found
Member since Feb 1, 2021
Pinned posts:
Replies:

I would use Dynamic Arrays and Dynamic Objects to make this simpler. Those two are your best friends when working with JSON.

ClassMethod GetAllPersons() As %Stream.Object
{
    d ..%SetContentType("application/json")
    Set rset = ##class(dc.Sample.Person).ExtentFunc()

    set stream=##class(%Stream.TmpCharacter).%New()
    set dynArray = [].%New()
    While rset.%Next() {
        do ##class(dc.Sample.Person).%OpenId(rset.ID).%JSONExportToString(.myPerson)
        Set dynObj = {}.%FromJSON(myPerson)
        do dynArray.%Push(dynObj)
    }
    d stream.Write(dynArray.%ToJSON())
    return stream
}

I know this has already been answered several ways, but let's not overlook embedded SQL as an option.

&sql(select JSON_OBJECT('Name':Name,'Title':Title,'Company':Company,'Phone':Phone, 'DOB':DOB) INTO :person WHERE ID = 1)
set personobj = {}.%FromJSON(person)

This can get a little unruly for tables with a lot of columns, but if you're wanting to pick out certain specific columns or customize the JSON field names, this approach gives you that flexibility.

Open Exchange applications:
Certifications & Credly badges:
David has no Certifications & Credly badges yet.
Global Masters badges:
Followers:
Following:
David has not followed anybody yet.