· Nov 29, 2019

How could we create a JSON which works on version 2016.1 ‽‽‽



We need to send data to an API using form_data as follows:


Using Ensemble 2017 we would use dynamic objects as:

set body={}.%Set("app_id",pRequest.appId).%Set("contents",contents).%Set("include_player_ids",pRequest.idsDispositivos)


However, how could we create a JSON to include into the form-data value??? We would like the following result: {"language1":"message1","language2":"message2"}


First we thought about creating it directly, however it stills being a dynamic object and wouldn't work with Ensemble 2016.2

set body = {
"app_id": "...",
"include_player_ids": ["..."],
"contents": {"en": "NEXT APPOINTMENT", "es": "SIGUIENTE CITA"}

Then, we thought about creating it a string and then somehow converting it to JSON:


//set tData("contents") = "{"_pRequest.idioma_":"_pRequest.mensaje_"}" //Convert to JSON?


I have read:

And tried the following commands into the terminal with Ensemble 2016.2


USER>set object = ##class(%Object).$new()
USER>set = "Stefan Wittmann"
USER>do object.$toJSON()
{"name":"Stefan Wittmann"}

And applied to our case:

USER>set message = ##class(%Object).%New()
USER>set message.en = "english message"
USER>set = "mensaje espanol"
USER>do message.$toJSON()
{"en":"english message","es":"mensaje espanol"}

So our code would be:

set tFormVarNames = "app_id,contents,include_player_ids"

set tData("app_id") = pRequest.idApp
set object = ##class(%Object).%New()
set object.en = pRequest.mensaje
set = pRequest.mensaje
set tData("contents") = object.%ToJSON()
set tData("include_player_ids") = pRequest.idsDispositivos

set ..Adapter.SSLConfig = "comunicacionMSSSI" set tSC = ..Adapter.SendFormDataArray(.tResponse,"POST",httpRequest,.tFormVarNames,.tData,"")


How would you achieve the required behaviour??? To create a JSON which would work on Ensemble 2016, with the structure:


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

Hello Eduard, I have tried your answer, however it outputs:

ESBSSCC>set enMsg = "hello"
ESBSSCC>set esMsg = "hola"
ESBSSCC>set obj = {"language1":(enMsg),"language2":(esMsg)}
ESBSSCC>write obj.%ToJSON()
ESBSSCC>w $classname(obj)

Being the version:

w $zversion
Cache for UNIX (Red Hat Enterprise Linux for x86-64) 2016.1.2 (Build 206U) Mon Jul 25 2016 16:47:58 EDT

And the documentation for the class %Library.Object, shows no method called %ToJSON()

Could you help me please?