Question
· 4 hr ago

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,

Product version: IRIS 2024.2
$ZV: HealthShare Provider Directory 2024.2.0 Build: 1009 [HealthShare Modules: Core:28.0 + Provider Directory:28.0] - IRIS for UNIX (Red Hat Enterprise Linux 8 for x86-64) 2024.1 (Build 267_2_24113U) Wed Jul 2 2025 16:36:55 EDT [Core:28.0|Provider Directory:28
Discussion (3)3
Log in or sign up to continue

To handle the JSON response and extract specific values into a Data Class structure and display them in the Trace Viewer, consider the following:

  1. Populating JSON into a Defined Class Structure: You need to map the JSON response to an object-based structure by using %JSON.Adaptor in 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.

  2. 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 DTL transformation:

    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)
    
  3. 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 id or name into Trace Viewer during execution [1][2][3].

  4. 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