Object: JsonParser
- Updated2024-09-12
- 3 minute(s) read
JSON Parser > Objects > Object: JsonParser
Object: JsonParser
The JsonParser object provides methods for reading and writing JSON files in UTF8 format and for converting a JSON object into a Dictionary object and vice versa. Use the CreateJsonParser command to create the JsonParser object.
The following example creates a JSON object from a dictionary object containing a dictionary object, an array, a time value, and primitive data types in the items, and displays the contents of the JSON object in the log area:
| VBScript | Python |
Dim oJsonParser, oMySubobject, oMyMainObject, sJsonString Set oJsonParser = CreateJsonParser() Set oMySubobject = CreateObject("Scripting.Dictionary") Set oMyMainObject = CreateObject("Scripting.Dictionary") Call oMySubobject.Add("intVal", CInt(1)) Call oMySubobject.Add("doubleVal", CDbl(1.2)) Call oMySubobject.Add("stringVal", "TextA") Call oMyMainObject.Add("subobject", oMySubobject) Call oMyMainObject.Add("array", Array("TextB", 5678, True)) Call oMyMainObject.Add("timestamp", oJsonParser.CreateDateTime(Now, True)) sJsonString = oJsonParser.Serialize(oMyMainObject, True) Call LogFileWrite(sJsonString)
The following example creates from a JSON object a dictionary object containing text, numbers, and another dictionary object. The example displays values of the dictionary object in the log area:
| VBScript | Python |
Dim oJsonParser, oVbsObject, sJsonString, sPath, oAggregates, sAvg Set oJsonParser = CreateJsonParser() sJsonString = "{""path"":""task.id.592d7b9e-7816-4517-973d-ef03cc814e35"",""current"":{""timestamp"":""2018-11-27T12:33:27Z""," & _ """value"":{""type"":1,""value"":""47""}},""aggregates"":{""count"":1,""avg"":47.0,""min"":""47"",""max"":""47""}}" Call oJsonParser.Deserialize(sJsonString, oVbsObject) sPath = oVbsObject.Item("path") Set oAggregates = oVbsObject.Item("aggregates") sAvg = oAggregates.Item("avg") sJsonString = oJsonParser.Serialize(oVbsObject, True) ' make pretty for viewing Call LogFileWrite(sJsonString) Call LogFileWrite("path: " & sPath & vbCRLF & "avg: " & sAvg)
The following example creates a JSON object from a dictionary object containing an array and a time value in the items. The example saves the JSON object in a UTF-8 file and displays the path in the log area:
| VBScript | Python |
Dim oJsonParser, oMyMainObject, sJsonString Set oJsonParser = CreateJsonParser() Set oMyMainObject = CreateObject("Scripting.Dictionary") Call oMyMainObject.Add("array", Array("TextB", 5678, True)) Call oMyMainObject.Add("timestamp", oJsonParser.CreateDateTime(Now, True)) sJsonString = oJsonParser.Serialize(oMyMainObject, True) Call oJsonParser.WriteFile(sJsonString, DataWritePath & "JsonObject.json") Call LogfileWrite(DataWritePath & "JsonObject.json")
The following example reads a JSON file, converts the JSON string into a dictionary object or an array, and displays the size of the dictionary object or array and the JSON string in the log area:
| VBScript | Python |
Dim oJsonParser, sFile, sJsonString, oVbsVariant Set oJsonParser = CreateJsonParser() sFile = DataWritePath & "JsonObject.json" sJsonString = oJsonParser.ReadFile(sFile) Call oJsonParser.Deserialize(sJsonString, oVbsVariant) Select Case VarType(oVbsVariant) Case vbObject Call LogfileWrite("Dictionary size: " & oVbsVariant.Count) Case vbArray + vbVariant Call LogfileWrite("Array size: " & UBound(oVbsVariant)+1) Case Else Call MsgBox("Some other data type") End Select Call LogfileWrite(sJsonString)
Methods
CreateDateTime | Deserialize | ParseDateTime | ReadFile | Serialize | WriteFile