· Jul 20, 2016

ConvertJSONToObject not working when 1st element of a list is blank


I am new to Cache and I am trying to convert a JSON string (msgdata) that contains a JSON payload to an object to be saved into a Driver. Entity table.  I am running into 2 issues and would appreciate any help:

1) It seems that when the 1st element of a list within the payload (Choices) is an empty string, after I call %ConvertJSONToObject, the obj.%data("payload").Choices contains just 1 empty element, removing the next 3 valid values (10,20,30). The same does not happen if I change the input to [10,20,"",30], in this case it works correctly.

2) The Min and Max values on my input = null and after calling %WriteJSONStreamFromObject with "aeqtw" format parameters, I get Min="" and Max="" but I need to return null instead.

thank you!

Code is below:

s msgdata="{"entityId":"test1","typeId":1,"name":"AssayName1","sourceId":"A","targetId":"L","userName":"","status":"N","identifiers":[],"payload":{"Name":"aSelNum","Choices":["",10,20,30],"DefaultChoice":"","Min":null,"Max":null}}"

s ok=##class(%ZEN.Auxiliary.jsonProvider).%ConvertJSONToObject(msgdata,,.obj)

s entity=##class(Driver.Entity).%New()

 s stream=##class(%GlobalBinaryStream).%New()

s ok=##class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject( stream,$g(obj.%data("payload")),"","",,"aeqtw")

d entity.payload.CopyFrom(stream)

q entity.%Save()

w stream.Read()
returns: {<cr><lf><ht>"Choices":[""<cr><lf><ht>],<cr><lf><ht>"DefaultChoice":"",<cr><lf><ht>"Max":"",<cr><lf><ht>"Min":"",<cr><lf><ht>"Name":"aSelNum"<cr><lf>}

Discussion (3)0
Log in or sign up to continue

%ZEN.proxyObject works alright with first empty element in a list. Here's a sample:

set json="{""Choices"":["""",10,20,30]}"
do ##class(%ZEN.Auxiliary.jsonProvider).%ConvertJSONToObject(json,,.obj)
do ##class(%ZEN.Auxiliary.jsonProvider).%ObjectToJSON(obj)


As for converting persistent objects to/from json, I would recommend first getting an example of json:

set obj = ##class(Driver.Entity).%OpenId(id)
do ##class(%ZEN.Auxiliary.jsonProvider).%ObjectToJSON(obj)

Would output json from object of  Driver.Entity class. Then you can modify your json, so it would have the same structure. The important part is that json should contain _class property = Driver.Entity, this way %ConvertJSONToObject knows which class to convert json into.