Article
· Sep 17, 2020 5m read

Native API for ObjectScript Demo

The demo is based on the raw class descriptions.
The data classes used are Address, Person, Employee, Company
For a more attractive demo, a JSONtoString method by ID was added.

After installation with ZPM just run from Terminal

USER>do ##class(ONAPI.demo).Run()
Adjust Parameters
host[127.0.0.1]:
port[1972]:
namespace[USER]:
user[_SYSTEM]:
pwd[SYS]:
timeout[5]:
****** connected ********

Next, you get a list of possible demo actions.
No input means no action.
The menu loops until you exit.

Populate Person by:100
     100
Populate Company by:10
     10
Populate Employee by:50
     50
Show Person by ID:3
{
  "Name":"O'Donnell,Mark I.",
  "SSN":"871-87-4555",
  "DOB":"1934-05-24",
  "Home":{
    "Street":"3012 Elm Drive",
    "City":"Ukiah",
    "State":"IN",
    "Zip":"11758"
  },
  "Office":{
    "Street":"1326 Maple Street",
    "City":"Jackson",
    "State":"MD",
    "Zip":"61987"
  },
  "FavoriteColors":[
    "Green"
  ],
  "Age":91
}
Show Company by ID:3
{
  "Name":"TeleData Gmbh.",
  "Mission":"Experts in innovative nano-connectivity for social networks.",
  "TaxID":"E4116",
  "Revenue":33297336,
  "Employees":[
    {
      "Name":"Larson,Dave G.",
      "SSN":"761-57-3123",
      "DOB":"2011-09-28",
      "Home":{
        "Street":"6346 Second Street",
        "City":"Chicago",
        "State":"NJ",
        "Zip":"16814"
      },
      "Office":{
        "Street":"6702 Clinton Drive",
        "City":"Tampa",
        "State":"CT",
        "Zip":"91275"
      },
      "Spouse":{
        "Name":"Willeke,Rhonda A.",
        "SSN":"434-63-3541",
        "DOB":"1974-02-15",
        "Home":{
          "Street":"1910 Oak Blvd",
          "City":"St Louis",
          "State":"ND",
          "Zip":"62884"
        },
        "Office":{
          "Street":"5148 Ash Court",
          "City":"St Louis",
          "State":"DE",
          "Zip":"36764"
        },
        "FavoriteColors":[
          "White",
          "Yellow"
        ],
        "Age":51
      },
      "FavoriteColors":[
        "Black"
      ],
      "Age":14,
      "Title":"Product Specialist",
      "Salary":28870
    },
    {
      "Name":"Quincy,Mo V.",
      "SSN":"345-36-6735",
      "DOB":"1945-05-14",
      "Home":{
        "Street":"399 Oak Court",
        "City":"Youngstown",
        "State":"LA",
        "Zip":"75634"
      },
      "Office":{
        "Street":"6307 Clinton Avenue",
        "City":"Oak Creek",
        "State":"NH",
        "Zip":"85911"
      },
      "Spouse":{
        "Name":"Iacobelli,Barb E.",
        "SSN":"951-91-9488",
        "DOB":"2020-09-04",
        "Home":{
          "Street":"8413 Elm Blvd",
          "City":"Denver",
          "State":"ID",
          "Zip":"91025"
        },
        "Office":{
          "Street":"4276 Oak Court",
          "City":"Oak Creek",
          "State":"ID",
          "Zip":"20879"
        },
        "Age":5
      },
      "FavoriteColors":[
        "Blue"
      ],
      "Age":80,
      "Title":"Global Sales Rep.",
      "Salary":89381
    }
  ]
}
Show Employee by ID:103
{
  "Name":"Larson,Dave G.",
  "SSN":"761-57-3123",
  "DOB":"2011-09-28",
  "Home":{
    "Street":"6346 Second Street",
    "City":"Chicago",
    "State":"NJ",
    "Zip":"16814"
  },
  "Office":{
    "Street":"6702 Clinton Drive",
    "City":"Tampa",
    "State":"CT",
    "Zip":"91275"
  },
  "Spouse":{
    "Name":"Willeke,Rhonda A.",
    "SSN":"434-63-3541",
    "DOB":"1974-02-15",
    "Home":{
      "Street":"1910 Oak Blvd",
      "City":"St Louis",
      "State":"ND",
      "Zip":"62884"
    },
    "Office":{
      "Street":"5148 Ash Court",
      "City":"St Louis",
      "State":"DE",
      "Zip":"36764"
    },
    "FavoriteColors":[
      "White",
      "Yellow"
    ],
    "Age":51
  },
  "FavoriteColors":[
    "Black"
  ],
  "Age":14,
  "Title":"Product Specialist",
  "Salary":28870
}
Show Global PersonD by ID:4
     $Data()=1
     Value=$lb("","Eastman,Mary C.","887-18-3730",44711,$lb("3889 Ash Blvd","Washington","TX",67862),$lb("5709 Oak Blvd","Chicago","IL",30845),"","")

Index list for Person & Employee (n,y):y
     $Employee
     $Person
     NameIDX
     SSNKey
     ZipCode
Exit Demo (n,y,*):y
****** done ********
 
USER>

GitHub

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

I could not compile demo.cls unless I commented out some lines:

But I was able to run:  do ##class(ONAPI.demo).Run() and fill in database with data. Thank you Robert!

I'll appreciate your support for the following compilation errors.  I'm on IRIS 2025.1  and  VS Code.

lines 67-73

    //read !,"Show Person by ID:",pop,!

    //if pop do jsn(..iris.ClassMethodValue("ONAPI.Person","JSON",pop))

    //read !,"Show Company by ID:",pop,!

    //if pop do jsn(..iris.ClassMethodValue("ONAPI.Company","JSON",pop))

    //read !,"Show Employee by ID:",pop,!    

    //if pop do jsn(..iris.ClassMethodValue("ONAPI.Employee","JSON",pop))

    //read !,"Show Global PersonD by ID:",pop,!

and

lines 94-97

   //jsn(jsn)

    //if $e(jsn)="{" write $$Do^ZPretty(jsn)

    //else  write "<UNDEFINED>"

    //quit

Error1:

Compilation started on 12/29/2025 14:37:50 with qualifiers 'cuk'

Compiling class ONAPI.demo

Compiling routine ONAPI.demo.1

ERROR: ONAPI.demo.cls(Menu+9) #8: <NOLINE> [Menu+8^ONAPI.demo.1]

 TEXT:     if pop do jsn(..iris.ClassMethodValue("ONAPI.Person","JSON",pop))

Detected 1 errors during compilation in 0.050s.

Error2:

Compilation started on 12/29/2025 14:38:41 with qualifiers 'cuk'

Compiling class ONAPI.demo

Compiling routine ONAPI.demo.1

ERROR: ONAPI.demo.cls(Menu+35) #1026: Invalid command : 'jsn(jsn)' : Offset:7 [Menu+34^ONAPI.demo.1]

 TEXT:     jsn(jsn)

Detected 1 errors during compilation in 0.023s.

My guess youw workaroud ERROR 2 cause ERROR 1

ERROR 2:
Your import of the code is just WRONG.   Wherever you may have it copied  it from

jsn(jsn) 
	if $e(jsn)="{" write $$Do^ZPretty(jsn)
	else  write "<UNDEFINED>"
	quit }

 jsn(jsn)  is a Label with a parameter

But if your COPY/ TEXT manipulator doesn't understand ObjectScript and 
inserts a BLANK before jsn(jsn)  ,  it's no longer a label anymore but just nonsens 
therefore :

ERROR: ONAPI.demo.cls(Menu+35) #1026: Invalid command : 'jsn(jsn)' :
Offset:7 [Menu+34^ONAPI.demo.1]

The compiler is right ! Your code is broken

ERROR 1:
now as the label jsn doesn'1 exist anymore, this MUST be an ERROR

 if pop do jsn(..iris.ClassMethodValue("ONAPI.Person","JSON",pop))

ERROR: ONAPI.demo.cls(Menu+9) #8: <NOLINE> [Menu+8^ONAPI.demo.1] 
TEXT:     if pop do jsn(..iris.ClassMethodValue("ONAPI.Person","JSON",pop))

The compiler is right again! Your code is broken. The label jsn() is destroyed

In Intersystems ObjectScript blanks and tabs are not just for illustration
or visualization but are an essential part of the language with some functionality.
Somehow like exact indenting in Python, just ~60 years longer

In my Studio such typing errors are flagged red immediately during typing.
So I wonder where the obviously broken source came from and how you worked on it

Dear Robert,

thank you for clarification, sir. So now it is obvious that my VS Code has spoiled (by its constant IntelliSense) the original demo.cls. It was compiling well before. Now I just replaced  demo.cls from original objectscript-native-api-0.0.1.tgz and it compiles. However, I see the warning about ^ZPretty, supposedly ZPretty.mac routine, which is not included in the archive.  I be honest, I don't remember where I download it. Now I checked with your github space https://github.com/r-cemper and don't see the Native API for ObjectScript repository at all.

In Intersystems ObjectScript blanks and tabs are not just for illustration
or visualization but are an essential part of the language with some functionality.

Now I've learned it! Thank you.

Please share the latest actual Native API for ObjectScript repository.

the one is mentioned in the README https://github.com/r-cemper/ObjectScript-Native-API.git does not work anymore.

This Demo database is very useful for starting! The demo.cls have filled up globals with test data for Person and Company in second. I'd be happy to have more classes and relations Demo database for my project. 

In my Studio such typing errors are flagged red immediately during typing.

Please share if Cache Studio works with IRIS 2025.1 and if, where can I obtain it.

Sincerely,

Dmitry

ZPretty is not in OEX anymore for various version conflicts
But in IPM you may find this workaround left over.

user>zpm "install pretty-json-upd" 

For this and other incompatibilities introduced by version changes the
repo on GitHub is broken and not available until several fixes.
Workaround for the left over version is ZPM once again

USER>zpm "install objectscript-native-api"


Studio-2024.1.5.649.0-win.msi  is still available from WRC download for contracted customers
It works perfectly for me on  IRIS for Windows (x86-64) 2025.1

Great! I don't even imagine that IRIS can install packages out of community repository pm.community.intersystems.com. Thank you for encouraging me, Robert!. ZMP was not on my system but after some playing around 

and ONAPI package is compiled in my namespace! I don't see no changes in demo.cls about ^ZPretty but please keep me updated if you make a fix. It is not critical now. I like the Demo database. It is a good classical case of entity-relationships.

Happy New Year, Robert!

Happy New Year!
Here is a draft version of ZPretty.mac for ISIS
I'm not sure if this is worth to be public at all.

ZPretty ; make JSON readable
#; input= JSON string 
Do(input) public {
    if $e(input)="{" do ##class(%JSON.Formatter).%New().Format(input)
    else  write "<UNDEFINED>"
    quit ""
    }
tst  public ; 
    set jsn={"Name":"Li,Robert K.","SSN":"672-92-9664","DOB":"1975-01-12","Home":{"Street":"986 Washington Blvd","City":"Boston","State":"PA","Zip":"95802"},"Office":{"Street":"6012 First Place","City":"Reston","State":"MT","Zip":"77739"},"Spouse":{"Name":"Avery,Zelda H.","SSN":"323-13-7437","DOB":"1943-03-27","Home":{"Street":"196 Main Drive","City":"Youngstown","State":"WY","Zip":"53229"},"Office":{"Street":"4056 Franklin Court","City":"Bensonhurst","State":"IA","Zip":"27688"},"FavoriteColors":["Black"],"Age":77},"Age":45,"Title":"Associate Marketing Manager","Salary":10421}
    quit jsn 

Happy New Year, Robert!

since my IRIS does not allow the 'public' word, I had to refactor the code this way:

Method jsn(jsn As %String = "")
{
    if $e(jsn)="{" {
        do ..ZPretty(jsn)
    } else {
        write "<UNDEFINED>"
    }
    quit
}

Method ZPretty(input As %String = "") As %String
{
    if $e(input)="{" {
        do ##class(%JSON.Formatter).%New().Format(input)
    } else {
        write "<UNDEFINED>"
    }
    quit ""
}

/// list of prepared tests	
Method Menu(ByRef go As %Boolean = 1)
{
	read !,"Populate Person by:",pop,!
	if pop write ?5,..iris.ClassMethodValue("ONAPI.Person","Populate",pop)
	read !,"Populate Company by:",pop,!
	if pop write ?5,..iris.ClassMethodValue("ONAPI.Company","Populate",pop)
	read !,"Populate Employee by:",pop,!
	if pop write ?5,..iris.ClassMethodValue("ONAPI.Employee","Populate",pop),!
	read !,"Show Person by ID:",pop,!
    if pop do ..jsn(..iris.ClassMethodValue("ONAPI.Person","JSON",pop)) 
	read !,"Show Company by ID:",pop,!
    if pop do ..jsn(..iris.ClassMethodValue("ONAPI.Company","JSON",pop))
	read !,"Show Employee by ID:",pop,!    
    if pop do ..jsn(..iris.ClassMethodValue("ONAPI.Employee","JSON",pop))
    read !,"Show Global PersonD by ID:",pop,!
    if $l(pop) {
	    set d=..iris.IsDefined("ONAPI.PersonD",$s(pop:pop,1:""))
	    write ?5,"$Data()=",d,!
	    if d#10 {
		    set Value=..iris.Get("ONAPI.PersonD",pop)
		    write ?5 zwrite Value  
    		}
		}
	read "Index list for Person & Employee (n,y):",pop,! 		
	if $l(pop),"Yy"[$e(pop) {
		set idx=""
		for  { 
			set idx=..iris.GetNext(1,,,"ONAPI.PersonI",idx)
			if $l(idx) write ?5,idx,!
			else  quit	
			} 
		}
	read "Exit Demo (n,y,*):",pop,!		
	if $l(pop),"*Yy"[$e(pop) set go=0
	quit
}