Question
· 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


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

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