Written by

Sales Engineer at InterSystems Corporation
Question Fabio Goncalves · Mar 21, 2016

Create dynamic %Object with object ID

Hi Community,

How can I create a JSON with the object ID by using the %Object:$toJSON API?

I have the following piece of code bellow where I create an object reference, create a %Object instance from %fromObject and them I am creating the JSON from $toJSON, but it does not expose the %ID (object id). How is the best approach to create a JSON with the respectives object IDs?

Try{Set tPersonObj = ##class(Data.Person).%OpenId(pId)If $Get(%objlasterror) $$$ThrowStatus(%objlasterror) Set tObj = ##class(%Object).$fromObject(tPersonObj)Write tObj.$toJSON()}Catch ex{Set tSC = ex.Status}

 

The outcome is:

Write tObj.$toJSON()
{"Age":28,"MaritalStatus":"Casada","Name":"Emilia de Souza","Sex":"F"}

I am expecting this:

{"ID":5,"Age":28,"MaritalStatus":"Casada","Name":"Emilia de Souza","Sex":"F"}

Thanks.

Comments

Timothy Leavitt · Mar 21, 2016

In 2016.2, you can do this by overriding %ToDynamicObject in Data.Person as follows:

/// In addition to the default behavior, also set the ID property to ..%Id()
Method %ToDynamicObject(target As %Object = "", ignoreUnknown = 0) [ ServerOnly = 1 ]
{
    set:target="" target = {}
    set target.ID = ..%Id() //Set ID property first so it comes at the beginning of the JSON output.
    do ##super(.target,.ignoreUnknown)
}
0
Fabio Goncalves  Mar 21, 2016 to Timothy Leavitt

Thank you very much.

Worked well.

0
Timothy Leavitt · Mar 21, 2016

A few side notes...

The correct/best way to create a %Object from a %RegisteredObject (or vice versa) is $compose, not $fromObject (which has been marked as Internal in more recent builds). This is first available in 2016.2.

SAMPLES>set person = ##class(Sample.Person).%OpenId(1)
SAMPLES>set obj = {}.$compose(person)
SAMPLES>w obj.$toJSON()
{"Age":88,"DOB":31520,"FavoriteColors":["Blue"],"Home":{"City":"Youngstown","State":"CO","Street":"1360 Oak Avenue","Zip":74578},"Name":"Tillem,Terry Y.","Office":{"City":"Gansevoort","State":"KY","Street":"4525 Main Court","Zip":93076},"SSN":"132-94-8739"}

Also, you can get %RegisteredObjects as JSON more directly:

SAMPLES>set person = ##class(Sample.Person).%OpenId(1) 
SAMPLES>w person.$toJSON()
{"Age":88,"DOB":31520,"FavoriteColors":["Blue"],"Home":{"City":"Youngstown","State":"CO","Street":"1360 Oak Avenue","Zip":74578},"Name":"Tillem,Terry Y.","Office":{"City":"Gansevoort","State":"KY","Street":"4525 Main Court","Zip":93076},"SSN":"132-94-8739"}
0
Amutha Raju · Apr 11, 2016

how to remove the "children" tag name from the json output. can anyone know ?
i need:
[{"ID":1,"Age":21,"EmailID":"","FirstName":"Bhagath","HomeAddress":"","LastName":"Singh"},{"ID":2,"Age":21,"EmailID":"","FirstName":"Bhagath","HomeAddress":"","LastName":"Singh"},]
but i got :
{"children":[{"ID":1,"Age":21,"EmailID":"","FirstName":"Bhagath","HomeAddress":"","LastName":"Singh"},{"ID":2,"Age":21,"EmailID":"","FirstName":"Bhagath","HomeAddress":"","LastName":"Singh"},]}

0
Fabio Goncalves  Apr 11, 2016 to Amutha Raju

Please, attach your code that generates this output.

Are you using some provider?

0
Amutha Raju · Apr 15, 2016

 set tmp = ##class(%ZEN.Auxiliary.jsonSQLProvider).%WriteJSONFromSQL(, "select * from Models.Person")
set st = ##class(Workflow.jsonProvider).%ObjectToJSON(tmp)
return st

this is my code which i used for fetch all data from table. correct me if anything wrong , i m new to intersystems. is any possibility to fetch all record through object without write full query?

Output with null 

{
"children":[
{"ID":1,"Age":21,"EmailID":"","FirstName":"Bhagath","HomeAddress":"","LastName":"Singh"}
,{"ID":2,"Age":21,"EmailID":"","FirstName":"Bhagath","HomeAddress":"","LastName":"Singh"}
,{"ID":3,"Age":21,"EmailID":"","FirstName":"Bhagath","HomeAddress":"","LastName":"Singh"}
,{"ID":4,"Age":21,"EmailID":"","FirstName":"Bhagath","HomeAddress":"","LastName":"Singh"}
,{"ID":5,"Age":21,"EmailID":"","FirstName":"Bhagath","HomeAddress":"","LastName":"Singh"}
,{"ID":6,"Age":21,"EmailID":"","FirstName":"Bhagath","HomeAddress":"","LastName":"Singh"}
]
}
null
0
Pavel Pogorelov  Apr 27, 2016 to Amutha Raju

I subscribe to the issue. 

Also, how can I use 

do obj.%ToDynamicObject(jsonObj)

in Cache Studio 2016.1?

0