Published on InterSystems Developer Community (https://community.intersystems.com)

Home > Creating JSON objects from ObjectScript objects

Question
Raj Singh · Jun 9, 2020

Creating JSON objects from ObjectScript objects

Say I have an ObjectScript object called Book. It has 2 properties title and author. It extends JSON.%Adaptor, so I can call book.%JSONExport() and get this output:

{ "title": "For Whom the Bell Tolls", "author": "Hemmingway" }

In my web service I want to have a search function that returns an array of Books along with the total number of responses, like this: 

{
    "results": 3,
    "items": [
        { "title": "For Whom the Bell Tolls", "author": "Hemmingway" },
        { "title": "The Invisible Man", "author": "Ellison" }, 
        { "title": "Tender is the Night", "author": "Fitzgerald" }
    ]

}

What's the best way to do this? Can I export an ObjectScript object into a Dynamic Object?

#JSON #ObjectScript #InterSystems IRIS #InterSystems IRIS for Health

Source URL:https://community.intersystems.com/post/creating-json-objects-objectscript-objects