Question
Rui Figueiredo · Nov 19, 2018

JSON Syntax

Hi,

I'm using InterSystem Cache v2015.2.5 and I'm implementing a REST API and returning JSON.

Following the documentation I see examples like this

Set obj = {"destinations": ["London","Madrid","Tokyo"]}
Write obj.%ToJSON()

However, I have a compilation error 

ERROR #1054: Invalid expression : '{"destinations":' 

Is there something that needs to be enabled on Studio to allow this syntax?

Thank you,

Rui


0
0 367
Discussion (5)2
Log in or sign up to continue

Using %DynamicObject throws an exception at runtime

ERROR #5002: Cache error: <CLASS DOES NOT EXIST>zGetED0005A+84^xxxxxxx *%Library.DynamicObject

I think you need to work with this

 set obj = ##class(%DynamicObject).%New()

do obj.Destinations.SetAt("London", counter)
Add to this obj your destinations as you would add to an array property and then do ToJson

After some investigation here is some code that works on v2015.2

// to create a dynamic array
SET results = ##class(%ListOfDataTypes).%New() 

// to create a dynamic object
SET obj = ##class(%ZEN.proxyObject).%New()   
SET obj.name = "John doe"

// add obj to the array
results.Insert(obj)  

// create dynamic response object
SET response = ##class(%ZEN.proxyObject).%New()
SET response.Results = results

Write response.%ToJSON()

---------------------------------------------

{   
    "Results": [
        {
            "name": "John Doe"
        }
     ]
}

Thanks,

Rui