JSON Response Help - Extracting Data into Data Class or Iteration to Extract Data
I have always struggled with Iterating through a JSON response to pull out certain fields into a Data Class Structure to use to populate fields in a DTL. So I defined the whole structure for the following JSON, with the base structure extending Ens.Response, %XML.Adaptor, and %JSON.Adaptor.
{
"status": {
"message": "success",
"code": 200
},
"data": {
"client": "xxxxxxxxxxxxxxxxxxxxxxxx",
"entities": [
{
"id": "xxxxxxxxxxxxxxxxxx`",
"name": "xxxxxxxxxxxxxxxxx",
"totalCommentCount": 272,
"totalRatingCount": 396,
"totalSurveyCount": 396,
"overallRating": {
"name": "Overall Satisfaction",
"value": 4.9,
"categoryResponseCount": 396,
"questionRatings": [
{
"name": "Provider listen carefully to you",
"value": 5.0,
"responseCount": 395
},
{
"name": "Provider expl in way you understand",
"value": 5.0,
"responseCount": 396
},
{
"name": "Talk with provider re prob/concern",
"value": 4.5,
"responseCount": 394
},
{
"name": "Know important info medical history",
"value": 4.9,
"responseCount": 393
},
{
"name": "Give easy to understand instruction",
"value": 4.9,
"responseCount": 347
},
{
"name": "Spend enough time with you",
"value": 4.9,
"responseCount": 395
},
{
"name": "Show respect for what you say",
"value": 5.0,
"responseCount": 395
}
]
},
"comments": [],
"PGSealOfIntegrity": {
"dataIntegrity": {
"horizontal": {
"darkBackground": {
"png": "xxxxxx"
},
"lightBackground": {
"jpg": "xxxxxxxxxxxxxxxxx",
"png": "xxxxxxxxxxxxxxxx"
},
"darkBackgroundHighResolution": {
"png": "xxxxxxxxxxxxxx"
}
},
"stacked": {
"darkBackground": {
"png": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"x
},
"lightBackground": {
"jpg": "xxxxxxxxxxxxxxxxx",
"png": "XXXXXXXXXXXXXXX"
}
}
},
"sealOfIntegrity": {
"darkBackground": {
"jpg": "XXXXXXXXXXXXXXXXXXXXXXXX",
"png": "XXXXXXXXXXXXXXXXXXXXXXXX"
},
"lightBackground": {
"jpg": "XXXXXXXXXXXXXXXXXXXXXXXXXX",
"png": "XXXXXXXXXXXXXXXXXXXXXXXXXX"
}
}
},
"totalPages": 14,
"ratings": []
}
],
"filter": {
"minThreshold": 0.0,
"minRatingCount": 30,
"minSurveyCount": 0,
"minWordCount": 0,
"days": 365,
"page": 1,
"perPage": 20,
"showComments": false,
"showRatings": true,
"configVersion": "v2",
"period": {
"to": "2026-02-05T23:59:59Z",
"from": "2025-02-05T00:00:00Z"
}
}
}
}The rest of the structure that is referenced by the base Response.cls structure extends %RegisteredObject, %JSON.Adaptor.
I am able to make the REST call, and the Response is populating into the Response.cls, however within the Trace Viewer the Response is blank. I know the data is there because when I send it to a DTL, I can pull the data elements out.
How do I make the Response show up within the Trace Viewer, or how would I go about extracting the values in RED below
"id": "xxxxxx",
"name": "xxxxxx",
"totalCommentCount": 272,
"totalRatingCount": 396,
"totalSurveyCount": 396,
"overallRating": {
"name": "Overall Satisfaction",
"value": 4.9,
Comments
To handle the JSON response and extract specific values into a Data Class structure and display them in the Trace Viewer, consider the following:
-
Populating JSON into a Defined Class Structure: You need to map the JSON response to an object-based structure by using
%JSON.Adaptorin classes. To do so, make sure all fields in your JSON are represented in the class structure. Create proper mapping with class schema that extends%JSON.Adaptor. -
Transforming Data Using DTL:
- Build your Data Transformation Logic (DTL) by associating the source JSON structure to the target structure.
- The JSON schema can be imported into targeted classes using tools like JSON2Persistent, or you can manually define the structure.
Example of integrating
DTLtransformation:set reader=##class(%XML.Reader).%New() set status=reader.OpenString(rsXML) do reader.Correlate("ReportMsg","package.source.ReportMsg") do reader.Next(.sourceObject,.status) set targetClassObject=##class(package.target.ReportMsg).%New() set result=##class(DTL.transformation).Transform(sourceObject,.targetClassObject) -
Use Trace in DTL: To make the extracted data visible in Trace Viewer, you can use the
<trace>element within your DTL transformation. Example:<trace value='"Extracted ID: " & sourceObject.id' /> <trace value='"Extracted Name: " & sourceObject.name' />This will print the values of
idornameinto Trace Viewer during execution [1][2][3]. -
Dynamic Object Parsing using ObjectScript: If you want to parse certain JSON fields dynamically:
set jsonObj = {}.%FromJSON(yourJsonData) write jsonObj.data.entities.%Get(0).id ; This extracts the first entity's `id` write jsonObj.data.entities.%Get(0).overallRating.name ; Extract `overallRating.name`
By defining a proper structure, mapping it correctly, and using the trace tag in DTL transformations, you can ensure both visibility in Trace Viewer and efficient data handling.
Sources:
"The rest of the structure that is referenced by the base Response.cls structure extends %RegisteredObject, %JSON.Adaptor."
A %RegisterdObject is not persistent, so it's never stored and therefore you cannot view in trace something that...does not exists (anymore).
Maybe an option could be to use %SerialObject instead of %RegisterdObject
Updated all the Child Data Structures to be %SerialObjects instead of %RegisteredObjects, but that did not make a difference. I am still not seeing anything within the Trace Viewer when the Response is sent back to the Process.
Add %XML.Adaptor to the superclasses of all your classes, the trace message content viewer represents the class as XML.
Have you tried the JSON to object library?