JSON deserializer

Completing the JSON deserializer/serializer

I was missing being able to deal with arrays and could only deal with strings in the first version of this, but have finally figured out how to deal with this. By introducing a parent object for array members I was able to treat arrays the same way as a group of key value pairs. This means that a JSON string as follows

{ "hiddendata": {
        "lastaccess": {
                "username": [
                        "k47",
                        99
                  ],
                  "startedat": "2/7/2011 11:55:13 PM",
                  "finishedat": "2/7/2011 11:46:55 PM"
         },
         "summary": {
                   "timeopen": "474474",
                   "countopen": "9"
           }
      }
}

 

translates into a cJobject that looks like this

hiddendata                              
hiddendata.lastaccess                    
hiddendata.lastaccess.username          
hiddendata.lastaccess.username._array    
hiddendata.lastaccess.username._array.1    fhk647
hiddendata.lastaccess.username._array.2    99
hiddendata.lastaccess.startedat            2/7/2011 11:55:13 PM
hiddendata.lastaccess.finishedat           2/7/2011 11:46:55 PM
hiddendata.summary                      
hiddendata.summary.timeopen                474474
hiddendata.summary.countopen               9

and can be accessed in various ways such as the below

set cj = new cObject
cj.deserialize(JSONstring)
With cj.child("lastaccess")
        .child("username._array").children(1).asString
        .child("username").child("_array").child("2").asLong
        .child("startedat").asDate
        .child("finishedat").Value
end With
With cj.child("summary")
        .child("timeopen").value
        .child("countopen").value
end With

 

I haven’t bothered with date decoding for now. Next step is to test all this with a whole bunch of JSON examples

About brucemcp 225 Articles
I am a Google Developer Expert and decided to investigate Google Apps Script in my spare time. The more I investigated the more content I created so this site is extremely rich. Now, in 2019, a lot of things have disappeared or don’t work anymore due to Google having retired some stuff. I am however leaving things as is and where I came across some deprecated stuff, I have indicated it. I decided to write a book about it and to also create videos to teach developers who want to learn Google Apps Script. If you find the material contained in this site useful, you can support me by buying my books and or videos.