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
Hi Rui,
This JSON feature is only available from 2016.1 onward.
Take a look at this post for an alternative way to output JSON
https://community.intersystems.com/post/zenauxiliaryjsonprovider-object-json-serialization
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
Add to this obj your destinations as you would add to an array property and then do ToJson
Ok version issue
i am using 2017.2.2
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