· Oct 4, 2017

Using $fromObject throws <SYNTAX> error

I'm simply trying to evaluate what is available in terms of JSON projection of registered objects within Caché.

I search through the group and found examples of object script that looked promising,


however, when I try to using the object script I get a <SYNTAX> error,

I have to two Caché builds that I'm evaluating with:

  • Cache for Windows (x86-64) 2017.1 (Build 792) Mon Mar 20 2017 19:36:11 EDT
  • Cache for Windows (x86-64) 2016.2.1 (Build 803) Wed Oct 26 2016 12:43:35 EDT

the example I'm using comes straight from this group:

set person=##class(Sample.Person).%OpenId(1)
write person.$toJSON()

I also tried:

set pobj={}.$compose(person)

and this:

set pobj=##CLASS(%Object).$fromObject(person)

the above object scripts all throw a <SYNTAX> error,

not sure what I'm missing here, according to posts I've read in this group this syntax should be available in 2016.2 +


Discussion (11)2
Log in or sign up to continue

Hi Randall.

The early implementation of JSON support (which exposes system methods with a '$' prefix like '$toJSON()', etc), was deprecated after 2016.1 releases (which you are using).

Unfortunately the articles you are referencing where posted at the time the old syntax was supported, hence the <SYNTAX> error.

This post in the community explains the rationale for this change in more  detail.

This link is the on-line equivalent to the JSON documentation available for 2017.1, and covers all the new syntax you should be using.   In there you will find the correct methods to instantiate a dynamic object, and how to work with it.



the part about the syntax changing in versions 2016.2+,

however, the initial question, and perhaps i did not make it clear was a way to project an object (such as Samples.Person) to JSON /dynamic object.

the example in pre 2016.2 version was: 

set person=##class(Sample.Person).%OpenId(1)
write person.$toJSON()

however i have not found a "api" using dynamic objects to do the same, something like this:

set person=##class(Sample.Person).%OpenId(1)

write person.%toJSON()

other than using the %ZEN.Auxiliary.altJSONProvider methods,

which i can live with,

i could also create my own method to "walk" the class properties and build the json/dynamic object myself,

which brings me to a follow up question:

is there a way to add custom property parameters, not a class parameter but the parameters you would see in the inspector within studio for a given property: CALCSELECTIVITY, CAPTION, COLLATION, CONTENT, etc...

the idea i have would be to add parameters i can use to include/exclude various properties from code that is accessing data from the class based on user "level" or other criteria.


The following code has been tested on versions 2015.2 and 2017.2:

Class dc.demo Extends %RegisteredObject

Property f1 As %String;

Property f2 As %Integer;

Property f3 As %ListOfDataTypes;

Method GetTarget(
  ByRef pParms As %String,
  Output pObj As %RegisteredObjectAs %Status
  pObj = ..%New()
  i $g(pParms(1))="a" {
    pObj.f1 "Peter"
    pObj.f2 = 21
    pObj.f1 "Bob"
    pObj.f2 = 12
    pObj.f3 ##class(%ListOfDataTypes).%New()
  q $$$OK

ClassMethod Obj2Json()
  ;d ##class(dc.demo).Obj2Json()
  "1) ",stream.Read($$$MaxLocalLength),!
  "2) ",stream.Read($$$MaxLocalLength),!

ClassMethod Json2Obj()
  ;d ##class(dc.demo).Json2Obj()
  obj.f1," : ",obj.f3.Count(),!

  obj.f1," : ",obj.f3.Count(),!



1) {"f1":"Peter","f2":21,"f3":[]}
2) {"f1":"Bob","f2":12,"f3":["a","b","c"]}
Peter : 0
Bob : 3