how to convert JSON to object in cache 2010 version
Hello,
I'm trying to convert JSON response to object in cache 2010.
My Code is bellow:
set sc = ##class(%ZEN.Auxiliary.jsonProvider).%ParseJSON(resptext,,.pObject,1)
my JSON response
{"access_token":"DSAFFDSGFDGTRHTRHTRH","in":"2522","refresh":"","scope":"send","token_type":"Bearer"}
the result : "9@%ZEN.proxyObject"
Comments
Looks like it works correctly.
write pObjectoutputs the object oref.
If you want to display the whole object, use zwrite:
zwrite pObjectIf you need a specific property, access them like this:
write pObject."access_token"
write pObject.inHello Eduard! Thank you for feedback.
In my case it doesn't work, the output:
Set ^tk("object")=pObject
Set ^tk("access_token1")=pObject."access_token"
Set ^tk("access_token2")=pObject.in
the response:
^tk("respText")="{""access_token"":""4SDFDSFDSF-aSDASDASD"",""expires_in"":""3300"",""refresh_token"":"""",""scope"":""sms_send"",""token_type"":""Bearer""}"
^tk("access_token1")=""
^tk("access_token2")=""
^tk("object")="9@%ZEN.proxyObject"
What will the following code output?
USER><FONT COLOR="#0000ff">k </FONT><FONT COLOR="#800000">arr </FONT><FONT COLOR="#0000ff">d </FONT><FONT COLOR="#800000">pObject</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">%CopyToArray</FONT><FONT COLOR="#000000">(.</FONT><FONT COLOR="#800000">arr</FONT><FONT COLOR="#000000">) </FONT><FONT COLOR="#0000ff">zw </FONT><FONT COLOR="#800000">arr</FONT>
PS: "in" ≠ "expires_in"
set arr=""
d pObject.%CopyToArray(.arr)
Error: <UNDEFINED>*arr
What will the following code output?
<FONT COLOR="#0000ff">s </FONT><FONT COLOR="#800000">resptext</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#008000">"{""access_token"":""4SDFDSFDSF-aSDASDASD"",""expires_in"":""3300"",""refresh_token"":"""",""scope"":""sms_send"",""token_type"":""Bearer""}"
</FONT><FONT COLOR="#0000ff">s </FONT><FONT COLOR="#800000">sc </FONT><FONT COLOR="#000000">= </FONT><FONT COLOR="#000080">##class</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008080">%ZEN.Auxiliary.jsonProvider</FONT><FONT COLOR="#000000">).</FONT><FONT COLOR="#0000ff">%ParseJSON</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">resptext</FONT><FONT COLOR="#000000">,,.</FONT><FONT COLOR="#800000">pObject</FONT><FONT COLOR="#000000">,1)
</FONT><FONT COLOR="#0000ff">i $$$ISOK</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">sc</FONT><FONT COLOR="#000000">) </FONT><FONT COLOR="#800080">{
</FONT><FONT COLOR="#0000ff">w </FONT><FONT COLOR="#800000">pObject</FONT><FONT COLOR="#000000">,!!,
</FONT><FONT COLOR="#800000">pObject</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">"access_token"</FONT><FONT COLOR="#000000">,!,
</FONT><FONT COLOR="#800000">pObject</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">"expires_in"</FONT><FONT COLOR="#000000">,!
</FONT><FONT COLOR="#0000ff">k </FONT><FONT COLOR="#800000">arr </FONT><FONT COLOR="#0000ff">s </FONT><FONT COLOR="#800000">arr</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#008000">"" </FONT><FONT COLOR="#0000ff">d </FONT><FONT COLOR="#800000">pObject</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">%CopyToArray</FONT><FONT COLOR="#000000">(.</FONT><FONT COLOR="#800000">arr</FONT><FONT COLOR="#000000">) </FONT><FONT COLOR="#0000ff">zw </FONT><FONT COLOR="#800000">arr
</FONT><FONT COLOR="#800080">}</FONT><FONT COLOR="#0000ff">else</FONT><FONT COLOR="#800080">{
</FONT><FONT COLOR="#0000ff">d $system</FONT><FONT COLOR="#008080">.OBJ</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">DisplayError</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">sc</FONT><FONT COLOR="#000000">)
</FONT><FONT COLOR="#800080">}</FONT>
My result (2018.1):USER><FONT COLOR="#0000ff">d </FONT><FONT COLOR="#000000">^test</FONT> 1@%ZEN.proxyObject4SDFDSFDSF-aSDASDASD 3300 arr("access_token")="4SDFDSFDSF-aSDASDASD" arr("expires_in")=3300 arr("refresh_token")="" arr("scope")="sms_send" arr("token_type")="Bearer"
What does this output in a terminal:
set respText="{""access_token"":""4SDFDSFDSF-aSDASDASD"",""expires_in"":""3300"",""refresh_token"":"""",""scope"":""sms_send"",""token_type"":""Bearer""}"
set sc = ##class(%ZEN.Auxiliary.jsonProvider).%ParseJSON(respText,,.pObject,1)
zw pObjectI tried it on a later verison and got:
pObject=<OBJECT REFERENCE>[1@%ZEN.proxyObject]
+----------------- general information ---------------
| oref value: 1
| class name: %ZEN.proxyObject
| reference count: 2
+----------------- attribute values ------------------
| %changed = 1
|%data("access_token") = "4SDFDSFDSF-aSDASDASD"
|%data("expires_in") = 3300
|%data("refresh_token") = ""
| %data("scope") = "sms_send"
|%data("token_type") = "Bearer"
| %index = ""
+-----------------------------------------------------