Deserialize class from JSon with nodes null
Hi all,
We have a class response inherited from %JSON.Adaptor with the following properties, also dependent classes:
Kurro.app.Msg.Search.Response
Kurro.app.Msg.Common.SearchCriteria
Kurro.app.Msg.Common.Availability
Kurro.app.Msg.Common.Acts
Kurro.app.Msg.Common.Parameters
Note: I've not written all nodes.
When we call to the API of the provider, we receive the following JSon
JSon response from provider
{
"searchCriteria": [{
"professionalNif": null,
"specialtyCode": "SPCODE17",
"acts": [
{
"actcode": "ACTCODE20",
"actModality": null,
"actPreparation": null,
"actParameters": [
{
"parameterCode": "contraste",
"parameterValue": "false"
}
]
}
]
}],
"availability": [
{
"acts": [
{
"actcode": "ACTCODE20",
"actModality": "",
"actPreparation": null,
"actParameters": null
}
],
"slots": [
{
"day": "2024-02-15",
"hours": [
{
"hour": "2024-02-15T18:20+01:00",
"duration": 20
},
{
"hour": "2024-02-15T18:40+01:00",
"duration": 20
},
{
"hour": "2024-02-15T19:00+01:00",
"duration": 20
},
{
"hour": "2024-02-15T19:20+01:00",
"duration": 20
},
{
"hour": "2024-02-15T19:40+01:00",
"duration": 20
}
]
}
],
"reasonNotAvailability": null
}
]
}
And we use the following command to deserialize the response
set tHttpResponse = ##class(%Net.HttpResponse).%New()
set tSC = ..Adapter.SendFormDataArray(.tHttpResponse,verbo,tHttpRequest,,,tUrl)
set content = ""while (tHttpResponse.Data.AtEnd = 0) {
set content = content_tHttpResponse.Data.Read()
}
do pResponse.%JSONImport(content)As you can see, the provider has returned a null value in the availability.acts.actParameters node, instead of an array of data, and the deserialize of the JSon raises an error.
How we have to define our class to prevent that type of error? I mean, if the response is a null value, ignore this error and continue with the rest of the content.
Thanks in advance.
Comments
I'm getting an error:
ERROR #9406: Unexpected format for value of field, searchCriteria, using class base mappingAfter fixing the JSON, the error disappeared:
<FONT COLOR="#ff00ff">{</FONT><FONT COLOR="#008000">"searchCriteria"</FONT><FONT COLOR="#808080">: </FONT><FONT COLOR="#ff00ff">{..}}</FONT> ==> <FONT COLOR="#ff00ff">{</FONT><FONT COLOR="#008000">"searchCriteria"</FONT><FONT COLOR="#808080">: </FONT><FONT COLOR="#ff00ff">[{..}]}</FONT>
Also note the mapping between the fields professionalId/professionalNif and preparation/actPreparationSorry, it's a mistake. Indeed, I've forgotten write the [ ], but still the error.
I've fixed the JSon in the post.
Have you tried setting the parameter as %JSONIGNORENULL?
https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cl…