How can i convert my object in JSON ?

Hello,

I’m creating a REST API service but I need to convert my object in JSON.

The Class “A” is extended from another Class “B”:

Class message_B Extends Ens.Request
{

Property ClientId As %String(MAXLEN = "");

Property mesagge As %String(MAXLEN = "");

}
Class message_A Extends message_B
{
Property idDate As %String;

Property Datetime As %String;

Property time As %String;

​}

When I use:

set pRequest = ##Class(message_A).%New()
​do ##class(Ens.Util.JSON).ObjectToJSONStream(pRequest, .content)

This convert all properties  (A y B), but I want that only convert properties in A. I mean, in JSON appear all property, incluided ClientId, mesagge, however I want that this varibales not appear in my JSON.

How can i do ?

  • 0
  • 0
  • 792
  • 5
  • 1

Answers

You could create a temporary object and just put in the properties you want:

USER>s ret={}

USER>s ret.idDate="something"

USER>s ret.Datetime="something2"

USER>s ret.time="something3"

USER>w ret.%ToJSON()
{"idDate":"something","Datetime":"something2","time":"something3"}
USER>

Hello,

Firstly, thank for your answer.  I try this method, but my object isn't  %DynamicAbstractObject, for this reason not found using %ToJSON() :-(

 

But the new object you are creating with

s ret={}

is :)

You can do:
s ret={}
s ret.propa={}
s ret.propa.subprop="something"

Ok! I have just one more question ...
s ret = {}
s ret.IdClient= "123"
How can i do something like that ?:
s ret.client.name = "peter"
When i try ...

Comments