Question
· Oct 23, 2023

Iterating the properties of a %ZEN.proxyObject

I am making a POST request to an API. In the case of an error, I don't know what parameters the response JSON will contain (they will change depending on the error). I am converting the response stream to an object but then I need a way to loop through all the properties and access their values. The following code is making the POST request, reading the response, and trying to add all the error messages to an array. But I can't iterate over the properties of the tProxy.errors object because it is a %ZEN.proxyObject. Is there a better way to do this?

Set tSC=..Adapter.Post(.tHttpResponse, , tPayload)
Set msgData = tHttpResponse.Data.Read()
Set temp = ..JSONStreamToObject(msgData, .tProxy)

If (tProxy.errors '= "") {
    Set errMsgs = ##class(%ListOfDataTypes).%New()
    Set itr = tProxy.errors.%GetIterator()
    While itr.%GetNext(.key,.val) {
        Do errMsgs.Insert(val)
    }
}

An example of the response I'm expecting to receive would be something like - 
{
  "errors": {
    "param1": ["The param1 field is required."],
    "param2": ["The param2 field must be between 1 and 4."]
  },
  "title": "One or more validation errors occurred.",
  "status": 400

}

Where I want to capture the values "The param1 field is required." and "The param2 field must be between 1 and 4."

Product version: IRIS 2023.1
Discussion (2)1
Log in or sign up to continue