Iterating Ens.Request to create a JSON

Primary tabs

Let's say we have a class 

AbcRequest extends Ens.Request, PropertiesBaseAbstractClass(has all my properties common to all request)


Prop2 and so on


Now in my Business Operation 

I want to make a json dynamically from this request.

Yes obviously I can do is


Set requestDefinition = ##class(%Library.CompiledClass).%OpenId("MyCreateRequest")
Set JsonObject    = ##class(%DynamicObject).%New()        

        for i=1:1:requestDefinition.Properties.Count() {  
            Set key            = requestDefinition.Properties.GetAt(i).Name
            Set value        = $property(pRequest,requestDefinition.Properties.GetAt(i).Name)
            Set JsonObject.key = value
        Set requestString        = JsonObject.%ToJSON()

This makes a json {"key":""}








Take a look on %Dictionary.ClassDefinition class.

you may get all the properties from Properties  

CompiledClass gives all the properties. Found out after some research

If you're on pre 2019.1 you can use %ZEN.Auxiliary.jsonProvider and %ZEN.Auxiliary.altJSONProvider to convert arbitrary object to and from JSON.

If you're on 2019.1 you can use %JSON.Adaptor class which is similar to %XML.Adaptor.