Question
· 10 hr ago

How to convert persistent object to dynamic object

Hi devs!

Suppose I have an instance of a persistent class:

Set person = ##class(dc.Sample.Person).%OpenId(1)

Class is an ancestor of %JSON.Adapter.

How can I convert it to dynamic object with the properties that person.%JSONExport() provides?

{"Name":"Elon Mask","Title":"Associate Accountant","Company":"InterSystems","Phone":"799-933-5569","DOB":"1926-12-11"}

Couldn't find a right method. 

set dynObj = ##class(%ZEN.Auxiliary.altJSONProvider).%ObjectToAET(person) works, but adds id and classname, which I don't want to be added:

zw dynObj
dynObj={"_class":"dc.Sample.Person","_id":1,"Name":"Elon Mask","Title":"Associate Accountant","Company":"InterSystems","Phone":"799-933-5569","DOB":31390}  ;

Thoughts?

Product version: IRIS 2025.1
Discussion (3)3
Log in or sign up to continue

not just Sample.Person but also Sample.Address,
or whatever serial class you refer to require %JSON Adaptor.
then

set person=##class(Sample.Person).%OpenId(3)
>set sc=person.%JSONExportToString(.Jperson)
set zjson={}.%FromJSON(Jperson)
ZWRITE
Jperson="{"LIMIT":103,"Name":"O'Rielly,Xavier E.","SSN":"331-77-5308","DOB":"1957-05-26","Home":{"Street":"8757 Elm Place","City":"Miami","State":"FL","Zip":"92027"},"Office":{"Street":"413 First Drive","City":"Miami","State":"NH","Zip":"83830"},"Age":67,"RowVer":0}"
person=<OBJECT REFERENCE>[2@Sample.Person]
sc=1
zjson=<OBJECT REFERENCE>[13@%Library.DynamicObject]

and there is your dynamic object