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" 

  • 0
  • 0
  • 101
  • 5
  • 1

Answers

Looks like it works correctly.

write pObject

outputs the object oref.

If you want to display the whole object, use zwrite:

zwrite pObject

If you need a specific property, access them like this:

write pObject."access_token"

write pObject.in

Hello 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>arr pObject.%CopyToArray(.arrzw arr

PS: "in" ≠ "expires_in"

set arr=""
pObject.%CopyToArray(.arr)

Error: <UNDEFINED>*arr

What will the following code output?

resptext="{""access_token"":""4SDFDSFDSF-aSDASDASD"",""expires_in"":""3300"",""refresh_token"":"""",""scope"":""sms_send"",""token_type"":""Bearer""}"

sc ##class(%ZEN.Auxiliary.jsonProvider).%ParseJSON(resptext,,.pObject,1)
i $$$ISOK(sc{
  pObject,!!,
    pObject."access_token",!,
    pObject."expires_in",!
  arr arr="" pObject.%CopyToArray(.arrzw arr
}else{
  d $system.OBJ.DisplayError(sc)
}

My result (2018.1):

USER>^test
1@%ZEN.proxyObject
 
4SDFDSFDSF-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 pObject

I 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 = ""
+-----------------------------------------------------