How could we create a JSON which works on version 2016.1 ‽‽‽
Hello,
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: https://community.intersystems.com/post/introducing-new-json-capabiliti…
And tried the following commands into the terminal with Ensemble 2016.2
USER>set object = ##class(%Object).$new()
USER>set object.name = "Stefan Wittmann"
USER>do object.$toJSON()
{"name":"Stefan Wittmann"}
USER>
And applied to our case:
USER>set message = ##class(%Object).%New()
USER>set message.en = "english message"
USER>set message.es = "mensaje espanol"
USER>do message.$toJSON()
{"en":"english message","es":"mensaje espanol"}
USER>
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 object.es = 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,"https://onesignal.com/api/v1/notifications")
How would you achieve the required behaviour??? To create a JSON which would work on Ensemble 2016, with the structure:
{"language1":"message1","language2":"message2"}
Comments
set enMsg = "hello"
set esMsg = "hola"
set obj = {"language1":(enMsg),"language2":(esMsg)}
write obj.%ToJSON()
Outputs:
{"language1":"hello","language2":"hola"}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()
WRITE obj.%ToJSON()
^
<METHOD DOES NOT EXIST> *%ToJSON,%Library.Object
ESBSSCC>w $classname(obj)
%Library.Object
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?
First of all I encourage you to update to 2016.2 or a later version, because JSON syntax differs considerably on 2016.1. Since 2016.2 we have a syntax-stable JSON API.
Anyway, on 2016.1 you can do this:
write obj.$toJSON()