Jasenko Donlagic · Jul 3, 2018

JSON Parsing and Streaming

Hi everyone,

I am still learning the platform for a student project and have to do some streaming and data analysis next. Since for my case I have no "live api" I wanted to just stream json files and output the data as it comes in from the files. (basically to emulate a incoming data scenario)

So thanks to the documentation and community posts I have figured how to create a stream and read data from a JSON but since I'm also new to JSON I have some parsing problems. I don't know how to access subarrays/sub-objects via objectscript.

The structure of the JSON File, I will omit some data because its large and the "rowset" has much more data:


So far I have used this to just get the data to output:

set filename = "test.json"
set stream = ##class(%Stream.FileCharacter).%New()
set array = [].%FromJSON(stream)
set i = array.%GetIterator()

while i.%GetNext(.key , .value ) {

      write "key = "_key_" , value = "_value,!

key = resource , value = pbp

key = parameters , value = 499@%Library.DynamicObject

key = resultSets , value = 994@%Library.DynamicArray

So my questions are:

1. What would be a good way to access all the sub-objects and sub-arrays via objectscript?

2. The way I am trying to emulate it, is it possible to get each of the "rowset" one by one and as soon as one is read by the stream, to output it immediately?

0 1,814
Discussion (2)0
Log in or sign up to continue

Here's an example of accessing properties and array elements:

set jsonObj = [].%FromJSON(filename)
set i = jsonObj.resultSets.%GetIterator()
while i.%GetNext(.key , .resultSet ) {
    set i2 = resultSet.rowSet.%GetIterator()
    while i2.%GetNext(.key , .rowSet ) {
        write rowSet.%Get(0),!