Creating Json Array for API Call
Hello Community!
We have this json payload for an api request we are making.
{
"fromDirectAddress" : "john@meda.com",
"toDirectAddress" : "smith@domain.com",
"emailAttachmentList" : [
{
"attachmentClass" : "application/xml",
"attachmentContent" : "PHRlc3Q+TWVkQWxsaWVzPC90ZXN0Pg==",
"attachmentTitle" : "MR-Lumbar Spine without contrast_1",
"attachmentFileName" : "ClinicalDocument"
},
"messageId" : "urn:uuid:a2d59543-91f4-45a4-99ef-72d70da1e454"
}
I created two classes (see below) but I am having issues with setting the emailAttachmentList as an array.
Class Request.SendDirectMessageRequest Extends (%Persistent, %XML.Adaptor, %JSON.Adaptor)
{
Parameter %JSONNULL = 1;
Property fromDirectAddress As %String(%JSONFIELDNAME = "fromDirectAddress", MAXLEN = "");
Property toDirectAddress As %String(%JSONFIELDNAME = "toDirectAddress", MAXLEN = "");
Attempt 1 // Property emailAttachmentList As Request.EmailAttachment(%JSONFIELDNAME = "emailAttachmentList");
Attempt 2 // Property emailAttachmentList As List of Request.EmailAttachment(%JSONFIELDNAME = "emailAttachmentList");
Property messageId As %String(%JSONFIELDNAME = "messageId", MAXLEN = "");
Property xdm As %Boolean(%JSONFIELDNAME = "xdm");
}
Class Request.EmailAttachment Extends (Ens.Request, %JSON.Adaptor)
{
Parameter %JSONIGNOREINVALIDFIELD = 1;
Property attachmentFileName As %String(%JSONFIELDNAME = "attachmentFileName", MAXLEN = "");
Property attachmentTitle As %String(%JSONFIELDNAME = "attachmentTitle", MAXLEN = "");
Property attachmentClass As %String(%JSONFIELDNAME = "attachmentClass", MAXLEN = "");
Property attachmentContent As %String(%JSONFIELDNAME = "attachmentContent", MAXLEN = "");
}
Attempt 1 (highlighted in yellow in the Request.SendDirectMessageRequest class) gives us the error below but at least that’s a response back as the message does leave IRIS.
Error HttpResponse: {"error":"/emailAttachmentList expected type: JSONArray, found: JSONObject"}
Attempt 2 gives us this error and the message doesn’t make it past the router.
ERROR <Ens>ErrBPTerminated: Terminating BP APIRouter1 # due to error: ERROR <Ens>ErrException: <PROPERTY DOES NOT EXIST>zS4+9^FAIS.BPL.APIBPL.Thread1.1 *attachmentClass,%Collection.ListOfObj -- logged as '-' number - @' Set status=1,callrequest.emailAttachmentList.attachmentClass="application/xml"'
> ERROR <Ens>ErrException: <PROPERTY DOES NOT EXIST>zS4+9^FAIS.BPL.APIBPL.Thread1.1 *attachmentClass,%Collection.ListOfObj -- logged as '-' number - @' Set status=1,callrequest.emailAttachmentList.attachmentClass="application/xml"'
Please assist us to figure out what we need to change the code to. Thank you!
Comments
Hello @PhoebeK
If emailAttachmentList is a DynamicArray, then the following property declaration is correct:
Attempt 2// Property emailAttachmentList As List of Request.EmailAttachment(%JSONFIELDNAME = "emailAttachmentList");It looks like you're trying to set the value directly like:
callrequest.emailAttachmentList.attachmentClass = "application/xml"
However, since emailAttachmentList is a list, you need to either:
- Use %JSONImport(response) to populate the list properly (e.g., `callrequest.%JSONImport(response)`), or
- Loop through the list and set the property on each individual item, like this:
For i=1:1:callrequest.emailAttachmentList.Count() {
Set callrequest.emailAttachmentList.GetAt(i).attachmentClass = "application/xml"
}Hi @Ashok.Kumar. I am running into a challenge as I am using the request builder in a BPL so I am struggling to use option 2 since the code action isn't available. Open to any suggestions.
I think I figured it out @Ashok.Kumar. I used your first suggestion "- Use %JSONImport(response) to populate the list properly (e.g., `callrequest.%JSONImport(response)`), "Thanks!