nested json output using %Object's and %Array's (v2016.1)

JSON, Caché

I am playing with json output from result sets

I've generated a dynamic resultset and now doing the json and I can get Array's with nested Objects but I'm trying to get to Objects nested with another object

mainObj = ##class(%Object).$new()
set array=[]

  While (result.Next()) {
     s object = ##class(%Object).$new()
     s object.id = result.Data("ID"
     s object.reg = result.Data("Registration")
     s object.snNum = result.Data("SatNavVehNumber")
     do array.$push(object)
     }
  w !!,array.$toJSON()

 

[{"id":"78","reg":"HIRE VEH","snNum":"234"},{"id":"86","reg":"HJ65 HJK","snNum":"567"}]

what I actually want is

[{"data":{"id":"78","reg":"HIRE VEH","snNu m":"234"}},{"data":{"id":"86","reg":"HJ65 HJK","snNum":"568"}}]

 so that each entry in the result set has [{"data":{resultSetData}}, {"data":{resultSetData}}, {"data":{resultSetData}} ]

this would then give me the option to say {"fetchDate":"20160522", "data":{resultSetData}} and so on.

how do you use %Object's and %Array's to give (almost) unlimited nested capabilities

kevin

 
  • 0
  • 0
  • 650
  • 4
  • 1

Answers

 s array = []
 While (result.Next()) {
     s object = {}
     s object.data = {}

     s object.data.id = result.Data("ID")
     s object.data.reg = result.Data("Registration")
     s object.data.snNum = result.Data("SatNavVehNumber")
     do array.$push(object)
     }
  w !!,array.$toJSON()

This should produce the desired output.

Comments

I have not seen the        dot Value dot value             syntax used, but I do like the simplicity

I've also been playing all morning, and I've just got this to work

While (result.Next()) {
   ; ID FleetNumber Registration SatNavVehNumber
   mainObj= ##class(%Object).$new()
     s object = ##class(%Object).$new()
     s object.id = result.Data("ID"
     s object.reg = result.Data("Registration")
     s object.snNum = result.Data("SatNavVehNumber")

     mainObj.data = object     ; store all the resultSet into "data":....
  do array.$push(mainObj)

 

I think I prefer your method, but I've noted my new possible solution here as an alternative
any other solutions ?

kevin

I'm curious what is the point to write longer ##class(%Object).$new() instead of simple {} ?

I would write it like this:

    set array =  []

    while (result.Next()) {
        set object = { 
                "data":{
                    "id":result.Data("ID"),
                    "reg":result.Data("Registration"),
                    "snNum":result.Data("SatNavVehNumber")
                }
            }
          do array.$push(object)
    }

You can directly embed your values as Caché Object Script and that makes your code look pretty close like the desired outcome. This approach makes it very simple to build complex JSON structures and still know what you are doing.

Timur:  I originally took my code from Introducing new JSON capabilities in Cache 2016.1 article  on this website and having not played with the new json capabilities, started with this method.

the answers  comments clearly show there are many ways to write the same thing.

Stefan: I prefer legibility over brevity. and your solution does indeed help me visualise the end output. So my preferences is to adopt this version.

thanks for the answers.
Any other comments ?