Question Mikhail (VetsEZ) Akselrod · 3 hr ago

Code example to build a %Library.DynamicObject or a %Library.DynamicArray from Resultset followed by converting to JSON

Go to the original post@Mikhail (VetsEZ) Akselrod

Please, provide Code example to build a %Library.DynamicObject or a %Library.DynamicArray from Resultset followed by converting to JSON

Product version: IRIS 2023.1
$ZV: HealthShare Unified Care Record 2023.1.0 Build: 1006 Adhoc: 23407 [HealthShare Modules: Core:25.0] -

Comments

DC AI Bot · 3 hr ago Bot

To build a %Library.DynamicObject or %Library.DynamicArray from a ResultSet followed by converting to JSON, follow these steps:

  1. Loop through the ResultSet, constructing either a %Library.DynamicObject or %Library.DynamicArray as needed.
  2. Use the %ToJSON() method to convert the constructed dynamic object or array into JSON format.

Below is an example code snippet illustrating this process:

ClassMethod ResultSetToJSON() As %Status
{
    // Get your ResultSet - replace with appropriate query or structure initialization
    // For demonstration, let's assume we have a ResultSet named resultSet
    
    Set dynArray = ##class(%Library.DynamicArray).%New()

    // Example loop to process resultSet rows
    While resultSet.%Next()
    {
        // Extract data from the ResultSet row
        Set dynObj = ##class(%Library.DynamicObject).%New()
        Do dynObj.%Set("Column1", resultSet.Column1)
        Do dynObj.%Set("Column2", resultSet.Column2)
        Do dynArray.%Push(dynObj)
    }

    // Convert to JSON string
    Write dynArray.%ToJSON()
    
    Quit $$$OK
}

In this example:

  • Data is processed row by row from resultSet.
  • A %Library.DynamicObject is created for each row, and properties are populated with column data.
  • Each dynamic object is added to a %Library.DynamicArray.
  • The %Library.DynamicArray is converted to JSON using its %ToJSON() method.

Remember, similar steps can be followed for a %Library.DynamicObject if your JSON structure requires objects instead of arrays 1.


Sources:

0

I have a ResultSet but I don't know what are column names. How to substitute it's names in the example below?

0
Enrico Parisi  1 hr ago to Mikhail (VetsEZ) Akselrod

What type of ResultSet? Can you please provide the exact class name of the ResultSet you use?

0