Question
· Aug 29, 2023

building a JSON message

I am trying to create the JSON message below for a REST API:

Class REST.Test.Sample.ItemDetails Extends (%Persistent, %JSON.Adaptor)
{

Property ItemId As %String;

Property Item As list Of AllItems;

}

Class AllItems:

Class REST.Test.Sample.AllItems Extends (%JSON.Adaptor, Ens.Response)
{ Property ItemtId As %String;

Property itemName As %String(MAXLEN = 100);

Property itemStockNumber As %String(MAXLEN = 150);

Property itemType As %String;

Property itemPriority As %String;

Property itemDetailData As %String(MAXLEN = 10000);
}

I have a table with the data I want to export as a JSON message containing a list of the items. I can build the data in the second class, REST.Test.Sample.AllItems object. But now I am trying to build the REST.Test.Sample.ItemDetails Object that is a list of the data from the AllItems object. 

I am looping thru the table of all items and based on defined filters, pulling the data needed to populate the AllItems objects and I can write each object out with the %JSONExport() method. But I can't get each of the AllItems objects to populate the ItemDetails object. In my loop after the AllItems object is build I have tried D AllItems.Item.SetAt(AllItems,cnt) and it is not storing my data in the ItemDetails object. 

Any help is greatly appreciated. 

Product version: IRIS 2021.1
$ZV: IRIS for Windows (x86-64) 2021.1.2 (Build 338U) Tue Apr 12 2022 12:04:03 EDT
Discussion (7)1
Log in or sign up to continue

%JSON.Adaptor default manner is rid out the null value properties when %JSONExport method invokes. There is some default parameter are used to achieve this while exporting. You need to enable parameter in your class definition. The %JSONNULL parameter is used to export all the field and display even the property has null values. Once you have added the parameter and try export the itemdetails object

Parameter %JSONNULL As BOOLEAN = 1;
Class Samples.AllItems Extends (%JSON.Adaptor, Ens.Response)
{

Parameter %JSONNULL As BOOLEAN = 1;

Property ItemtId As %String;
Property itemName As %String(MAXLEN = 100);
Property itemStockNumber As %String(MAXLEN = 150);
Property itemType As %String;
Property itemPriority As %String;
Property itemDetailData As %String(MAXLEN = 10000);
}
{
    "ItemId": "test",
    "Item": [
        {
            "ItemtId": null,
            "itemName": null,
            "itemStockNumber": 123,
            "itemType": "test",
            "itemPriority": "High",
            "itemDetailData": null
        }
    ]
}

I set the parameter and am still not see the list of the items. in my code, I am looping thru the table and when I find an item that needs to be added to the JSON message, I set the properties in the AllItems JSON message then I am trying to add the data from that object to the ItemDetails object so the ItemDetails has all the table entries that are in the AllItems object. 

so I collect and build the AllItems object for each table entry and then want to put all the AllItems data into the ItemDetails and return this JSON to the user.

this is the class:

Class REST.Test.Sample.AllItems Extends (%JSON.AdaptorEns.Response)
Property ItemtId As %String;

Property itemName As %String(MAXLEN 100);

Property itemStockNumber As %String(MAXLEN 150);

Property itemType As %String;

Property itemPriority As %String;

Property itemDetailData As %String(MAXLEN 10000);
}

 

ClassMethod getItemData() As %Status [ PublicList = (U, ItemDetails, RESULTS) ]
{
Set tSC = $$$OK
results=""
               
s AllItems=##class(REST.Test.Sample.ItemDetails).%New()
(STATUS,XX)=0 F  S STATUS=$O(^GMR(123,"D",STATUS)) Q:STATUS'>0 f  s XX=$O(^GMR(123,"D",STATUS,XX)) Q:XX'>0 d
    .CONERR=""
    .GETS^DIQ(123,XX,"*","IE","ItemDetails","CONERR")
    .Q:"5,6"'[STATUS
    .Q:$P(^GMR(123,XX,0),"^",5)'=9
    .Item=..%New()
    .Item.ItemId=XX
    .CONID=XX_","
    .itemId=$g(ItemDetails(123,CONID,.02,"I"))
    .ien=XX_","
    .Item.ItemName=$g(ItemDetails(123,CONID,.02,"E"))
    .Item.itemStockNumber=$G(ItemDetails(123,CONID,10,"I"))
    .Item.itemType=$G(ItemDetails(123,CONID,10,"I"))
   .Item.itemPriorty=$G(ItemDetails(123,CONID,11,"I"))

    .X2=0 F  S X2=$O(ItemDetails(123,CONID,20,X2)) q:X2'>0 D
    ..results=$g(results)_"|"_ItemDetails(123,CONID,20,X2)
    ..Item.ItemData=$g(results)
    .;d Item.Consult.SetAt(XX,XX)
    .s AllItems.ItemId=XX
    .AllItems.Item.SetAt(Item.ItemName,"ItemName")

    d AllItems.%JSONExport()
    200