· Jul 14, 2017

Is there a way to map a $lb() to its data model ?

Hi all,


We faced a problem where we would like to map a $lb object to its corresponding data model structure in order to later on export it as JSON.


So imagin that we have something like this that actually maps to a persistent table with its structure. 


Is there a way to transform this $lb (without the need of openig the object itself) to a JSON object with the proper table fields as properties?


Many thanks

Discussion (9)2
Log in or sign up to continue

Jose, $lb() holds STORED values of you object instance. If your opened instance modified property(ies) then I see no point of looking into stored data, just serialize you IN-MEMORY values to JSON.

On the other hand, if a process A opened and modified instance of the object and without saving and you want to serialize the same instance from process B, then again, there is no difference between opening an object instance or using some custom serialization directly from stored data.

It is not good practice anyway to keep objects opened for a long time without saving their new values.

lastly, my comment about JSON array was that JSON array doesn't need to know the name in the name:value pair so you can easily write a custom JSON serializer to serialize directly $lb() into JSON array.



maybe I missed your original business case need, can you explain why current implementation is not good enough for you?

zw list


You can use value positioning to match their $list index according to their property definition index. But you will need to describe each property to detect their types and if they are instantiable. In case they do, you can use the value to open the instance and the property type. E.g:

set properties = ##class(%Dictionary.CompiledClass).%OpenId("SQLUser.Table").Properties

for i=1:1:propeties.Count() {
    set listValue = $lg(SQLUser^TableD(1) , i)

    set property = propeties.GetAt(i)
   // if property.Type  inherits from %RegisteredObject
   set value = $System.OBJ.OpenId(property.Type, listValue)
  // else 
   set value = listValue

If you don't want to open the class descriptor instance you can also use macros from But you will have to deal with $order.

My suggestion takes the longest path. You will need a method that supports each property type behavior,  like that lb(,,"bla","20050502123400").

But why can't you use an instance? Is it because the structure is too old and there isn't any %Persistent class to represent it?

Hi Daniel,

I would try to explain better:

The $lb it's an old value of one register of my table "Table" that is:  SQLUser^TableD(1) = $lb("",,,,,"",,,"BOOLEAN","0",2,2,"1",,"bla",$lb(,,"bla","20050502123400"),"",1,"bla",,0)  


As you can imagin, my table "Table" has its properties (Fields) that match the $lb fields. What I would like is to get a JSON object of those properties and the values of my old $lb so I have something like:














Kind regards