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:

{  
   "resource":"pbp",
   "parameters":{  
      "GameID":"0021600732",
      "StartPeriod":0,
      "EndPeriod":14
   },
   "resultSets":[  
      {  
         "name":"PbP",
         "headers":[  
            "GAME_ID"
         ],
         "rowSet":[  
            [  
               "0021600732"
            ],
            [  
               "0021600733"
            ]
         ]
      }
   ]
}

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,!
}

Output:
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
  • 0
  • 400
  • 1
  • 1

Answers

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()
    write resultSet.name,!
    while i2.%GetNext(.key , .rowSet ) {
        write rowSet.%Get(0),!
    }
}